Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use cases for the different Padrino haml helpers

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?

like image 644
ghostfly Avatar asked Jan 21 '11 04:01

ghostfly


1 Answers

Let's go through the use cases.

  1. content_for/yield_content

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.

  1. render/partial

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.

  1. capture_html/concat_content

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.

like image 187
nesquena Avatar answered Nov 15 '22 11:11

nesquena