What is the correct syntax for using the Rails helper `content_for` in a HAML context

What are the differences in use - and = in the following:

 - content_for :header do %h1 Title 

and

 = content_for :header do %h1 Title 

Which is the right way?

+9
ruby-on-rails haml
source share
2 answers

It depends on what you want to do.

To display the title immediately, do:

 = content_for :header do %h1 Title 

To save the content and use it later, do:

 - content_for :header do %h1 Title 

And use it somewhere in your views:

 = content_for :header 

In Rails <3.2, you needed to use = yield :header . This is still supported in Rails 3.2, but it doesn’t work in auxiliary modules, and content_for does (thanks @drewish).

+19
source share

= yield: the header, although not outdated, has become less useful. Although Rails 3.2 supports this, a problem arises in the helper modules. content_for, on the other hand, works well and is a more frequently used command.

0
source share

All Articles