rails yield与content_for

来源:互联网 发布:删除手机预装软件 编辑:程序博客网 时间:2024/06/08 11:26

yield 是你指明您的内容将如何放在一个布局中。

<div>  <h1> This is the wrapper!</h1>  <%= yield :my_content %></div>

content_for你指定的内容将被渲染到内容区域。

<% content_for :my_content do %>  This is the content.<% end %>

The result would be

<div>  <h1> This is the wrapper!</h1>  This is the content.</div>

他们在渲染过程的两端,与yield 规定内容去哪里,和content_for指定实际的内容是什么。

最好的方法是将yield 用在layout中,content_for 用在view中;content_for 有另外的一种用法,当不传block给它时,它将返回之前呈现的内容;这主要用在helper中,yeild不能工作;在view中,content_for 最好用于呈现内容,yield 最好用于调取内容。

原创粉丝点击