在HAML上下文中使用Rails`content_for`帮助器的正确语法是什么 [英] What's the right syntax to use Rails `content_for` helper in a HAML context
本文介绍了在HAML上下文中使用Rails`content_for`帮助器的正确语法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下使用-
和=
有什么区别:
What are the differences in the use of -
and =
in the following:
- content_for :header do
%h1 Title
和
= content_for :header do
%h1 Title
正确的方法是什么?
推荐答案
这取决于您要执行的操作.
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
在Rails中< 3.2您需要使用= yield :header
. Rails 3.2仍支持该功能,但是在content_for
可以的情况下,它在辅助模块中不起作用(感谢@drewish).
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).
这篇关于在HAML上下文中使用Rails`content_for`帮助器的正确语法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文