Rails:具有相似代码和结构的多种布局 - DRY?
下面的查看代码有点冗长,所以让我先陈述一下我的问题和疑问。我有多种布局,每种布局的代码略有不同。目前,我尝试通过使用部分来限制任何冗余。然而,我仍然想知道它是否可以更干燥。显然,保持事物尽可能干燥是核心原则之一,但我想知道在某些情况下,只要保持一致(即使不是干燥)仍然可以为您提供所需的效率和可维护性,需要付出多少努力。那么,鉴于此,您会推荐什么?顺便说一句,我的环境是 Rails 2.3.12。
# LAYOUT #1
<%= render :partial => 'shared/html_header' %>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<%= render :partial => "shared/head" %>
<body>
<%= render :partial => "shared/announcements" %>
<div id="outer">
<%= render :partial => 'shared/banner' %>
<%= render :partial => 'shared/nav' %>
<div id="main">
<table cellpadding="0" cellspacing="0">
<tr>
<td>
<%= render :partial => 'shared/content' %>
</td>
<td><%= spacer_img(:width => 10) %></td>
<td>
<%= render(:partial => 'shared/sidebar_a', :locals => {:box_color => 'greenBox'}) %>
<%= render(:partial => 'shared/sidebar_b', :locals => {:box_color => 'blueBox'}) %>
<%= render(:partial => 'shared/sidebar_c', :locals => {:box_color => 'redBox'}) %>
</td>
</tr>
</table>
</div>
<%= render :partial => 'shared/footer' %>
</div>
<%= render :partial => 'shared/google_analytics_script' %>
</body>
</html>
# LAYOUT #2
<%= render :partial => 'shared/html_header' %>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<%= render :partial => "shared/head" %>
<body>
<%= render :partial => "shared/announcements" %>
<div id="outer">
<%= render :partial => 'shared/banner' %>
<%= render :partial => 'shared/nav' %>
<div id="main">
<table cellpadding="0" cellspacing="0">
<tr>
<td>
<%= render :partial => 'shared/enticement_rendering' %>
<%= render :partial => 'shared/content' %>
</td>
<td><%= spacer_img(:width => 5) %></td>
<td>
<%= render(:partial => 'shared/sidebar_x', :locals => {:box_color => 'greenBox'}) %>
<%= render(:partial => 'shared/sidebar_y', :locals => {:box_color => 'blueBox'}) %>
<%= render(:partial => 'shared/sidebar_z', :locals => {:box_color => 'redBox'}) %>
</td>
</tr>
</table>
</div>
<%= render :partial => 'shared/footer' %>
</div>
<%= update_lb_javascript %>
<%= render :partial => 'shared/google_analytics_script' %>
</body>
</html>
The view code below is a bit verbose, so let me state my problem and question first. I have multiple layouts with somewhat slightly different code in each one. Currently, I've tried to limit any redundancy through the use of partials. However, I still wonder if it could be more DRY. Obviously, it's one of the core principles to keep things as DRY as possible, but I wonder how much effort it's worth in some cases where just being consistent (if even it's not DRY) still gives you the efficiency and maintainability that you need. So, given that, what would you recommend? Btw, my environment is Rails 2.3.12.
# LAYOUT #1
<%= render :partial => 'shared/html_header' %>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<%= render :partial => "shared/head" %>
<body>
<%= render :partial => "shared/announcements" %>
<div id="outer">
<%= render :partial => 'shared/banner' %>
<%= render :partial => 'shared/nav' %>
<div id="main">
<table cellpadding="0" cellspacing="0">
<tr>
<td>
<%= render :partial => 'shared/content' %>
</td>
<td><%= spacer_img(:width => 10) %></td>
<td>
<%= render(:partial => 'shared/sidebar_a', :locals => {:box_color => 'greenBox'}) %>
<%= render(:partial => 'shared/sidebar_b', :locals => {:box_color => 'blueBox'}) %>
<%= render(:partial => 'shared/sidebar_c', :locals => {:box_color => 'redBox'}) %>
</td>
</tr>
</table>
</div>
<%= render :partial => 'shared/footer' %>
</div>
<%= render :partial => 'shared/google_analytics_script' %>
</body>
</html>
# LAYOUT #2
<%= render :partial => 'shared/html_header' %>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<%= render :partial => "shared/head" %>
<body>
<%= render :partial => "shared/announcements" %>
<div id="outer">
<%= render :partial => 'shared/banner' %>
<%= render :partial => 'shared/nav' %>
<div id="main">
<table cellpadding="0" cellspacing="0">
<tr>
<td>
<%= render :partial => 'shared/enticement_rendering' %>
<%= render :partial => 'shared/content' %>
</td>
<td><%= spacer_img(:width => 5) %></td>
<td>
<%= render(:partial => 'shared/sidebar_x', :locals => {:box_color => 'greenBox'}) %>
<%= render(:partial => 'shared/sidebar_y', :locals => {:box_color => 'blueBox'}) %>
<%= render(:partial => 'shared/sidebar_z', :locals => {:box_color => 'redBox'}) %>
</td>
</tr>
</table>
</div>
<%= render :partial => 'shared/footer' %>
</div>
<%= update_lb_javascript %>
<%= render :partial => 'shared/google_analytics_script' %>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎唯一改变的是侧边栏框,这是正确的吗?
最简单的解决方案是提出一种在控制器中设置的配置,并且仅使用一个视图文件。
因此,在一个操作中:
在另一个操作中:
然后您的视图将变为:
It seems that the only thing that changes is the sidebar boxes, is this correct?
Easiest solution would be to come up with a configuration that gets set in the controller and just use one view file..
So in one action:
In another:
And then your view becomes: