Rails:具有相似代码和结构的多个布局 - DRY?

时间:2011-08-14 15:19:19

标签: ruby-on-rails model-view-controller layout dry

下面的视图代码有点冗长,所以让我先说明我的问题和疑问。我有多个布局,每个布局略有不同。目前,我试图通过使用partial来限制任何冗余。但是,我仍然想知道它是否会更干。显然,将内容尽可能保持干净是其中的核心原则之一,但我想知道在某些情况下,只要保持一致(即使它不是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>

1 个答案:

答案 0 :(得分:2)

似乎唯一改变的是侧边栏,这是正确的吗?

最简单的解决方案是提出一个在控制器中设置并只使用一个视图文件的配置。

所以在一个动作中:

@sidebars = [ {:name => 'sidebar_a', :color => 'blue'}, {:name => 'sidebar_b', :color => green} ]

在另一个:

@sidebars = [ {:name => 'sidebar_c', :color => 'blue'}, {:name => 'sidebar_d', :color => green} ]

然后您的观点变为:

<% @sidebars.each do |sidebar| %>
  <%= render(:partial => "shared/#{sidebar.name}", :locals => {:box_color => sidebar.color}) %>
<% end %>