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

非 Y 不嫁゛ 提交于 2019-12-22 04:01:19

问题


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?


回答1:


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




回答2:


= yield :header, although not deprecated, has been put to less use. Although Rails 3.2 supports this, but the problem occurs in helper modules. content_for , on the other hand does work well and is a more often used command.



来源:https://stackoverflow.com/questions/12843266/whats-the-right-syntax-to-use-rails-content-for-helper-in-a-haml-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!