I read http://www.padrinorb.com/guides/application-helpers but I'm unclear as to what are the use cases for each of the view helpers. Specifically, how do content_for/yield_content, render/partial, capture_html, and concat_content
all fit together?
Right now I've been using render 'my/view'
in my controllers and throwing in some =partial 'my/partial'
within 'my/view'
just to break apart the main template file into smaller chunks.
Is the right way to go about it? And when/where would I want to use the other helper functions?
Let's go through the use cases.
This is for injecting content into a layout file that might be useful. Example is adding additional css/scripts into a layout from another template. The example on the guide is the same, showing how to add CSS files to your layout from any template that requires them. You can also use it for adding content onto sidebars, additional links, etc. It is for things that do not require their own template but need to pass information back to a layout based on the view being shown.
render is for showing a given template associated with a route. render should be used for the main actions after a route is processed. partial is like a 'method' in a view. It can be reused and variables can be passed to change the output. You use partials in main templates to break up code and reuse pieces of views that otherwise might seem redundant.
This is usually used to create your own helpers that take blocks of content. For instance let's create a helper that takes a haml block and wraps it in a div. Usage is as follows:
# template.haml
# NOTE the equals so the content is returned
# and added to the view directly
= div_wrapper do
%h1 Some heading
%p This is now wrapped in a div
To implement this and use it in a template, you need to be able to 'capture' the haml passed into the block in order to process and then wrap a 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 into the view wrapped in a div. Now, lets presume we want this helper to be more complex and so you want the use to be more like this:
# template.haml
# NOTE the dash so the content is not outputted directly
- div_wrapper do
%h1 Some heading
%p This is now wrapped in a div
but it also works in other helpers:
# some_helper.rb
def example(name)
div_wrapper { "<h1>Test</h1>" }
end
In order to properly print out the wrapped content from the helper in both a template AND straight ruby, we can use concat_content and check to see if we need to 'concat' the result to the template or simply 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. The functions can overlap but generally it becomes clear when to use which based on specific context.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With