ker*_*lin 1 layout controller views ruby-on-rails dry
我有一个基于Rails的网站,基本的两列布局.右边是一个侧栏,有不同的(我们只是说)垂直小部件.根据您访问的页面,可以包含,排除和按不同顺序组成小组件.例如:
主页(应用)
PAGE内容......
SIDEBAR
第十页
PAGE内容......
SIDEBAR
第Y页
PAGE内容......
SIDEBAR
现在,我正在显示特定于页面的小部件的方式不是很干.我正在为每个页面创建一个布局,然后一遍又一遍地复制和粘贴相同的HTML部分和窗口小部件.例如:
application.html.erb
<%= render :partial => 'shared/html_header' %>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<%= render :partial => "shared/head" %>
<body>
<div id="outer">
<%= render :partial => 'shared/banner' %>
<%= render :partial => 'shared/nav' %>
<div id="main">
<table cellpadding="0" cellspacing="0" border="0" style="width:100%;">
<tr style="vertical-align:top;">
<td style="width:759px;">
<%= render :partial => 'shared/content' %>
</td>
<td><%= image_tag("spacer.gif", :width => "5") %></td>
<td style="width:252px;">
<%= render :partial => 'shared/widget_a' %>
<%= image_tag("spacer.gif", :height => "10") %>
<%= render :partial => 'shared/widget_b' %>
<%= image_tag("spacer.gif", :height => "10") %>
<%= render :partial => 'shared/widget_c' %>
<%= image_tag("spacer.gif", :height => "10") %>
</td>
</tr>
</table>
</div>
<%= render :partial => 'shared/footer' %>
</div>
<%= render :partial => 'shared/user_voice_widget_script' %>
<%= render :partial => 'shared/google_analytics_script' %>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我基本上只是为其他页面布局重复此模式,但使用特定于页面的小部件.似乎不太干.
我认为必须有一种更有效的方法来做到这一点.有什么建议?
您可以使用单个布局文件为所有页面定义两个列结构,然后使用Rail的content_for帮助程序在上下文中填充侧边栏,具体取决于所访问的页面.然后唯一的页面特定信息是侧边栏细节,而不是一遍又一遍地重复整个布局.
在您的(单个)应用程序布局中:
<td style="width:252px;">
<%= yield :sidebar %>
</td>
Run Code Online (Sandbox Code Playgroud)
在您页面的某个位置:
<% content_for :sidebar do %>
<%= render :partial => 'shared/widget_a' %>
<%= image_tag("spacer.gif", :height => "10") %>
...
<% end %>
Run Code Online (Sandbox Code Playgroud)
如果足够多的窗口小部件组合经常出现在一起,您可以考虑将它们的render指令放在一起并使用content_for块中的那些.
| 归档时间: |
|
| 查看次数: |
1502 次 |
| 最近记录: |