两部分 Rails 布局

发布于 2024-08-19 02:31:43 字数 169 浏览 4 评论 0原文

我的网页由两部分组成,比如说顶部和底部(页眉和页脚除外——这些部分在页面之间是一致的)。根据操作动态生成这些部分的最佳实践是什么?

我想出的一种方法是顶部视图和底部部分视图;在布局中,顶部调用yield,底部调用renderpartial。部分的名称根据操作动态替换。

不确定这是最好的方法。

My web pages consist of two parts, let's say top and bottom (except header and footer -- those are consistent across pages). What is the best practice to dynamically generate those parts depending on the action?

One approach I have come up with is to have view for the top and partial for the bottom; in the layout call yield for the top and render partial for the bottom. The name of the partial is dynamically substituted depending on the action.

Not sure it is the best way to do it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

一影成城 2024-08-26 02:31:43

我觉得你的想法很好。在你看来你可以这样做:

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

I think your idea is fine. In your views you could do:

<%- content_for :top do -%>
  […]
<%- end -%>

<%- content_for :bottom do -%>
  <%= render @partial_name %>
<%- end -%>

Of course you should check whether the partial exist and provide some default behavior. But I think you're aware of that anyway.

And then in your layout:

<div id="top">
  <%= yield :top %>
</div>

<div id="bottom">
  <%= yield :bottom %>
</div>
卖梦商人 2024-08-26 02:31:43

这是我过去使用过的视图 DSL 的一个非常简化的版本。对我们来说效果很好。实际上,我们参数化了辅助方法,这样我们就可以从许多布局部分中进行动态选择(使页面具有侧边栏、多列等)。

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

Here is a very simplified version of a view DSL I've used in the past. Worked well for us. In reality we parameterized the the helper methods so we could choose from many layout partials on the fly (to have pages with sidebars, multiple columns, etc).

# 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 %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文