Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the right syntax to use Rails `content_for` helper in a HAML context

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

- content_for :header do
  %h1 Title

and

= content_for :header do
  %h1 Title

What is the right way?

like image 917
Erik Escobedo Avatar asked Oct 11 '12 15:10

Erik Escobedo


1 Answers

That depends on what you want to do.

To render the header right away, do:

= content_for :header do
  %h1 Title

To store the content and use it later, do:

- content_for :header do
  %h1 Title

And to use it somewhere in your view(s):

= content_for :header

In Rails < 3.2 you needed to use = yield :header. That is still supported in Rails 3.2 but it doesn't work in helper modules while content_for does (thanks @drewish).

like image 127
gylaz Avatar answered Nov 16 '22 03:11

gylaz