同一页面上有多个 content_for

发布于 2024-11-01 09:02:11 字数 1220 浏览 0 评论 0原文

我的应用程序中有一大块 HTML,我想将其移至共享模板中,然后使用 content_for 和 Yield 来插入必要的内容。但是,如果我在同一个布局文件中多次使用它,则 content_for 只会附加到前一个布局文件中,从而使该想法无法很好地发挥作用。有解决办法吗?

<div class="block side">
    <div class="block_head">
        <div class="bheadl"></div>
        <div class="bheadr"></div>
        <h2><%= yield :block_head %></h2>
    </div>
    <div class="block_content">
        <%= yield :block_content %>
    </div>
    <div class="bendl"></div>
    <div class="bendr"></div>
</div>

我使用以下代码来设置块的内容

    <%= overwrite_content_for :block_head do -%>
        My Block
    <% end -%>
    <%= overwrite_content_for :block_content do -%>
        <p>My Block Content</p>
    <% end -%>
    <%= render :file => "shared/_blockside" %>

问题是,如果我在同一布局上多次使用此内容,则原始块的内容将附加到辅助块

我尝试创建一个自定义帮助器方法来解决它,但是它不会返回任何内容,

  def overwrite_content_for(name, content = nil, &block)
    @_content_for[name] = ""
    content_for(name, content &block)
  end

我也可能会完全错误地处理这个问题,如果有更好的方法让内容像这样工作,我想知道。谢谢。

I have large block of HTML in my application that I would like to move into a shared template and then use content_for with yields to insert the necessary content. However, if I use it more than once in the same layout file the content_for just appends to the previous making that idea not work so well. Is there a solution to this?

<div class="block side">
    <div class="block_head">
        <div class="bheadl"></div>
        <div class="bheadr"></div>
        <h2><%= yield :block_head %></h2>
    </div>
    <div class="block_content">
        <%= yield :block_content %>
    </div>
    <div class="bendl"></div>
    <div class="bendr"></div>
</div>

and I use the following code to set the content for the block

    <%= overwrite_content_for :block_head do -%>
        My Block
    <% end -%>
    <%= overwrite_content_for :block_content do -%>
        <p>My Block Content</p>
    <% end -%>
    <%= render :file => "shared/_blockside" %>

The problem is if I use this multiple times on the same layout the content from the original block is appended to the secondary block

I have tried creating a custom helper method to get around it, however it does not return any content

  def overwrite_content_for(name, content = nil, &block)
    @_content_for[name] = ""
    content_for(name, content &block)
  end

I may also be going about this completely wrong and if there are any better methods for getting content to work like this I would like to know. Thanks.

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

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

发布评论

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

评论(5

哆啦不做梦 2024-11-08 09:02:11

在 Rails 4 中,您可以传递 :flush 参数来覆盖内容。

<%= content_for :block_head, 'hello world', :flush => true %>

或者,如果您想通过一个块,

<%= content_for :block_head, :flush => true do %>
  hello world
<% end %>

请参见。此帮助程序的源代码了解更多信息细节

In Rails 4, you can pass the :flush parameter to override content.

<%= content_for :block_head, 'hello world', :flush => true %>

or, if you want to pass a block,

<%= content_for :block_head, :flush => true do %>
  hello world
<% end %>

cf. this helper's source code for more details

失眠症患者 2024-11-08 09:02:11

您应该将 overwrite_content_for 定义如下(如果我正确理解您的问题):

  def overwrite_content_for(name, content = nil, &block)
    content = capture(&block) if block_given?
    @_content_for[name] = content if content
    @_content_for[name] unless content
  end

请注意,如果您的块产生 nil,则旧内容将被保留。然而,整个想法听起来不太好,因为你显然做了两次渲染(或至少对象实例化)。

You should define your overwrite_content_for as the following (if I understand your question correctly):

  def overwrite_content_for(name, content = nil, &block)
    content = capture(&block) if block_given?
    @_content_for[name] = content if content
    @_content_for[name] unless content
  end

Note, that in case your block yields nil, then the old content will be retained. However, the whole idea doesn't sound good, as you're obviously doing some rendering (or at least object instantiation) twice.

路还长,别太狂 2024-11-08 09:02:11

您始终可以直接传递内容而不依赖于块:

<% content_for :replaced_not_appended %>

You can always just pass the content directly and not rely on a block:

<% content_for :replaced_not_appended %>
迷迭香的记忆 2024-11-08 09:02:11

我不确定我是否真的理解你的问题 - 这是一种有效的代码方法:

view:

<% content_for :one do %>
  Test one
<% end %>

<% content_for :two do %>
  Test two
<% end %>

<p>Hi</p>

application.html.erb

<%= yield :one %>
<%= yield %>
<%= yield :two %>

Railsguides: http://guides.rubyonrails.org/layouts_and_rendering.html#using-content_for

I am not sure if I really understand your question - here is one approach in code that works:

view:

<% content_for :one do %>
  Test one
<% end %>

<% content_for :two do %>
  Test two
<% end %>

<p>Hi</p>

application.html.erb

<%= yield :one %>
<%= yield %>
<%= yield :two %>

Railsguides: http://guides.rubyonrails.org/layouts_and_rendering.html#using-content_for

孤独难免 2024-11-08 09:02:11

您可以使用命名的 content_foryield 块,如下所示:

View:

<% content_for :heading do %>
  <h1>Title of post</h1>
<% end %>

<p>Body text</p>

<% content_for :footer do %>
  <cite>By Author Name</cite>
<% end %>

然后在布局中:

<%= yield :heading %>
<%= yield %>
<%= yield :footer %>

您当然可以按任何顺序定义它们。

文档: http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper .html

You can use named content_for and yield blocks like this:

View:

<% content_for :heading do %>
  <h1>Title of post</h1>
<% end %>

<p>Body text</p>

<% content_for :footer do %>
  <cite>By Author Name</cite>
<% end %>

Then in a layout:

<%= yield :heading %>
<%= yield %>
<%= yield :footer %>

You can of course define those in any order.

Docs: http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html

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