Use cases for different assistants of Hadla Padrino

I read http://www.padrinorb.com/guides/application-helpers , but I don’t understand what use cases for each of the view helpers. In particular, how do content_for/yield_content, render/partial, capture_html, and concat_content all fit together?

I am currently using render 'my/view' in my controllers and throwing a few =partial 'my/partial' inside 'my/view' to break the main template file into smaller pieces.

Is this the right thing to do? And when / where do I want to use other helper functions?

+6
ruby view haml padrino
source share
1 answer

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.

  1. 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.

  1. 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 # 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 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 # 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 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.

+12
source share

All Articles