Ruby on rails 两部分轨道布局
我的网页由两部分组成,比如说顶部和底部(除了页眉和页脚——它们在页面之间是一致的)。根据动作动态生成这些部件的最佳实践是什么 我提出的一种方法是,顶部有视图,底部有局部视图;在布局中,为顶部调用yield,为底部调用render partial。根据操作动态替换分部的名称Ruby on rails 两部分轨道布局,ruby-on-rails,layout,partials,Ruby On Rails,Layout,Partials,我的网页由两部分组成,比如说顶部和底部(除了页眉和页脚——它们在页面之间是一致的)。根据动作动态生成这些部件的最佳实践是什么 我提出的一种方法是,顶部有视图,底部有局部视图;在布局中,为顶部调用yield,为底部调用render partial。根据操作动态替换分部的名称 我不确定这是最好的方法。我认为你的想法很好。在您看来,您可以: <%- content_for :top do -%> […] <%- end -%> <%- content_for :bo
我不确定这是最好的方法。我认为你的想法很好。在您看来,您可以:
<%- content_for :top do -%>
[…]
<%- end -%>
<%- content_for :bottom do -%>
<%= render @partial_name %>
<%- end -%>
[…]
当然,您应该检查分部是否存在,并提供一些默认行为。但我想你已经意识到了这一点
然后在布局中:
<div id="top">
<%= yield :top %>
</div>
<div id="bottom">
<%= yield :bottom %>
</div>
这是我过去使用的视图DSL的一个非常简化的版本。这对我们很有效。实际上,我们对helper方法进行了参数化,这样我们就可以动态地从许多布局分区中进行选择(使页面具有边栏、多列等)
#app/views/shared/_screen.erb
'共享/屏幕')
结束
def屏幕标题
内容:屏幕标题do
产量
结束
结束
def屏蔽体
内容:屏幕内容
产量
结束
结束
def页脚
内容:页脚do
产量
结束
结束
结束
#app/views/layouts/application.erb
#仅显示body标签
对不起,一开始你的问题读得不够好。编辑了我的答案,这样它会和你们的问题有更多的关联。我喜欢你们的方法——会尝试一下。谢谢
# app/views/shared/_screen.erb
<div id="screen">
<div class="screen_header">
<%= yield :screen_header %>
</div>
<div class="screen_body">
<%= yield :screen_body
</div>
<div class="bottom">
<%= yield :footer %>
</div>
</div>
# app/helpers/screen_helper.rb
module ScreenHelper
def screen(&block)
yield block
concat(render :partial => 'shared/screen')
end
def screen_header
content_for :screen_header do
yield
end
end
def screen_body
content_for :screen_body do
yield
end
end
def footer
content_for :footer do
yield
end
end
end
# app/views/layouts/application.erb
# only showing the body tag
<body>
<%= yield :layout
<body>
# Example of a page
# any of the sections below (except screen) may be used or omitted as needed.
# app/views/users/index.html.erb
<% screen do %>
<% screen_header do %>
Add all html and/or partial renders for the header here.
<%end%>
<% screen_body do %>
Add all html and/or partial renders for the main content here.
<% end %>
<% footer do %>
Add all the html and/or partial renders for the footer content here.
<% end %>
<% end %>