Let's look at the use cases.
- content_for / yield_content
This is for injecting content into a layout file, which may be useful. Example: adding additional css / scripts to the layout from another template. The example in the manual is the same, showing how to add CSS files to the layout from any template that requires them. You can also use it to add content to sidebars, sitelinks, etc. This applies to those things for which you do not need your own template, but it needs to transfer information to the layout based on the displayed view.
- render / partial
render is intended to display a given template associated with a route. the render should be used for basic actions after processing the route. partial as a "method" in a view. It can be reused, and variables can be passed to change the output. You use partial files in the main templates to break the code and reuse fragments of views that might otherwise seem redundant.
- capture_html / concat_content
This is usually used to create custom helpers that take blocks of content. For example, create a helper that takes a haml block and wraps it in a div. The following is used:
# template.haml
To implement this and use it in a template, you must be able to βcaptureβ the haml passed to the block to process and then wrap the div around it. This is where capture_html comes in:
def div_wrapper(&block) nested_content = capture_html(&block) content_tag(:div, nested_content) end
This will take the content and spit it out in the form enclosed in a div. Now let's say that we want this helper to be more complex, and therefore you want it to be more similar:
# template.haml
but he also works in other assistants:
# some_helper.rb def example(name) div_wrapper { "<h1>Test</h1>" } end
In order to correctly print the wrapped content from the helper both in the template and in the direct ruby, we can use concat_content and check whether we need to "concatenate" the result with the template or just return it.
def div_wrapper(&block) nested_content = capture_html(&block) tag_result = content_tag(:div, nested_content) block_is_template?(block) ? concat_content(tag_result) : tag_result end
I hope this works as a basic overview. Functions may overlap, but it usually becomes clear when to use them based on a specific context.