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

发布于 2024-11-29 15:34:31 字数 2984 浏览 0 评论 0原文

下面的查看代码有点冗长,所以让我先陈述一下我的问题和疑问。我有多种布局,每种布局的代码略有不同。目前,我尝试通过使用部分来限制任何冗余。然而,我仍然想知道它是否可以更干燥。显然,保持事物尽可能干燥是核心原则之一,但我想知道在某些情况下,只要保持一致(即使不是干燥)仍然可以为您提供所需的效率和可维护性,需要付出多少努力。那么,鉴于此,您会推荐什么?顺便说一句,我的环境是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染火枫林 2024-12-06 15:34:31

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

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

因此,在一个操作中:

@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 %> 

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:

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

In another:

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

And then your view becomes:

<% @sidebars.each do |sidebar| %>
  <%= render(:partial => "shared/#{sidebar.name}", :locals => {:box_color => sidebar.color}) %>
<% end %> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文