<
html
>
< head >
< title >Shoppery - < %= yield(:title) || "The Place to Buy Stuff" % > </title>
< %= stylesheet_link_tag 'application' % >
< %= yield :head % >
</head>
< body >
< div id ="container" >
< p style ="color: green" > < %= flash[:notice] % > </p>
< h1 > < %= yield(:title) % > </h1>
< %= yield % >
</div>
</body>
</html>
< head >
< title >Shoppery - < %= yield(:title) || "The Place to Buy Stuff" % > </title>
< %= stylesheet_link_tag 'application' % >
< %= yield :head % >
</head>
< body >
< div id ="container" >
< p style ="color: green" > < %= flash[:notice] % > </p>
< h1 > < %= yield(:title) % > </h1>
< %= yield % >
</div>
</body>
</html>
这个是layout,application.rhtml,全局的渲染模板。
在title的地方预出了空。
在applicationhelper中
# application_helper.rb
def title(page_title)
content_for(:title) { page_title }
end
def title(page_title)
content_for(:title) { page_title }
end
在某个具体页面中这样写就好了,譬如在new.rhtml中
<
% title "New Product" %
>
---
我比较喜欢在controller中定义@title变量,在layout中写<%= @title || "default title"%>
本文转自 fsjoy1983 51CTO博客,原文链接:http://blog.51cto.com/fsjoy/131953,如需转载请自行联系原作者