我的网站上的所有页面都使用应用程序布局。我希望我的静态页面也使用静态页面布局。如何同时使用这两种布局?
应用程序布局:
<html>
<head></head>
<body>
<%= yield %>
</body>
</html>静态页面布局:
<div class="static">
</div>静态页面:
<p>Hello</p>我希望页面的结果是:
<html>
<head></head>
<body>
<div class="static">
<p>Hello</p>
</div>
</body>
</html>发布于 2013-05-30 13:17:30
来自http://guides.rubyonrails.org/layouts_and_rendering.html#using-nested-layouts
在第二个布局中,静态页面布局:
静态布局
<%= render :template => 'layouts/application' %>应用程序布局
<%= yield %>要在其中使用静态布局的控制器
class AController
layout 'static_layout'static_layout应该在视图/布局中。
我想你也可以使用布局约定:
class StaticController和一个文件app/views/layouts/static.html.erb
或在render调用中选择布局:
render 'something', layout: 'static'发布于 2013-05-30 13:20:30
您需要了解yield的用法
application.html.erb
<html>
<head></head>
<body>
<% if @static %>
<div class="static">
<%= yield :static %>
</div>
<% end %>
<%= yield %>
</body>
</html>static_page.html.erb
<% content_for :static do %>
<p>Hello</p>
<% end %>现在你需要小心在静态页面控制器操作中将@static设置为true
发布于 2013-05-30 13:02:20
在控制器中使用layout
class StaticPagesController < .....
# you can apply only,execpt if you want
layout 'static'
def home
end
endhttps://stackoverflow.com/questions/16828015
复制相似问题