Rails使用块进行部分渲染

时间:2010-06-01 15:36:50

标签: ruby-on-rails render partial block

我正在尝试重新使用我编写的提供面板样式的html组件。类似的东西:

  <div class="v-panel">
    <div class="v-panel-tr"></div>
    <h3>Some Title</h3>
    <div class="v-panel-c">
      .. content goes here
    </div>
    <div class="v-panel-b"><div class="v-panel-br"></div><div class="v-panel-bl"></div></div>
  </div>

所以我看到渲染需要一个块。我想我可以这样做:

# /shared/_panel.html.erb
<div class="v-panel">
  <div class="v-panel-tr"></div>
  <h3><%= title %></h3>
  <div class="v-panel-c">
    <%= yield %>
  </div>
  <div class="v-panel-b"><div class="v-panel-br"></div><div class="v-panel-bl"></div></div>
</div>

我想做类似的事情:

#some html view
<%= render :partial => '/shared/panel', :locals =>{:title => "Some Title"} do %>
  <p>Here is some content to be rendered inside the panel</p>
<% end %>

不幸的是,这不适用于此错误:

ActionView::TemplateError (/Users/bradrobertson/Repos/VeloUltralite/source/trunk/app/views/sessions/new.html.erb:1: , unexpected tRPAREN

old_output_buffer = output_buffer;;@output_buffer = '';  __in_erb_template=true ; @output_buffer.concat(( render :partial => '/shared/panel', :locals => {:title => "Welcome"} do ).to_s)
on line #1 of app/views/sessions/new.html.erb:
1: <%= render :partial => '/shared/panel', :locals => {:title => "Welcome"} do -%>
...

所以它显然不喜欢=一个块,但如果我删除它,那么它就不会输出任何内容。

有谁知道如何做我想在这里实现的目标?我想在我的网站上的许多地方重复使用这个面板html。

5 个答案:

答案 0 :(得分:188)

虽然上述两个答案都在起作用(好的是tony链接到的例子)我最终在上面的帖子中找到了最简洁的答案(Kornelis Sietsma的评论)

我猜render :layout 完全正是我想要的东西:

# Some View
<%= render :layout => '/shared/panel', :locals => {:title => 'some title'} do %>
  <p>Here is some content</p>
<% end %>

结合:

# /shared/_panel.html.erb
<div class="v-panel">
  <div class="v-panel-tr"></div>
  <h3><%= title -%></h3>
  <div class="v-panel-c">
    <%= yield %>
  </div>
</div>

答案 1 :(得分:20)

这是基于之前答案的替代方案。

shared/_modal.html.erb上创建部分:

<div class="ui modal form">
  <i class="close icon"></i>
  <div class="header">
    <%= heading %>
  </div>
  <div class="content">
    <%= capture(&block) %>
  </div>
  <div class="actions">
    <div class="ui negative button">Cancel</div>
    <div class="ui positive button">Ok</div>
  </div>
</div>

application_helper.rb上定义您的方法:

def modal_for(heading, &block)
  render(
    partial: 'shared/modal',
    locals: { heading: heading, block: block }
  )
end

从任何视图中调用它:

<%= modal_for('My Title') do |t| %>
  <p>Here is some content to be rendered inside the partial</p>
<% end %>

答案 2 :(得分:8)

您可以使用捕获助手,甚至可以在渲染调用中内联:

<%= render 'my_partial',
           :locals => { :title => "Some Title" },
           :captured => capture { %>
    <p>Here is some content to be rendered inside the partial</p>
<% } %>

并在共享/面板中:

<h3><%= title %></h3>
<div class="my-outer-wrapper">
  <%= captured %>
</div>

将产生:

<h3>Some Title</h3>
<div class="my-outer-wrapper">
  <p>Here is some content to be rendered inside the partial</p>
</div>

请参阅http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html

答案 3 :(得分:2)

根据接受的答案,这对我使用Rails 4的效果很好。

我们可以这样渲染一个面板:

= render_panel('Non Compliance Reports', type: 'primary') do
  %p your content goes here!

enter image description here

这需要一个帮助方法和一个共享视图:

帮助方法(ui_helper.rb)

def render_panel(heading, options = {}, &block)
  options.reverse_merge!(type: 'default')
  options[:panel_classes] = ["panel-#{options[:type]}"]

  render layout: '/ui/panel', locals: { heading: heading, options: options } do
    capture(&block)
  end
end

查看(/ui/panel.html.haml)

.panel{ class: options[:panel_classes] }
  .panel-heading= heading
  .panel-body
    = yield

答案 4 :(得分:1)

如果你先将它分配给变量然后输出它,我认为它会起作用(只是做了快速的脏测试)。

<% foo = render :partial => '/shared/panel', :locals =>{:title => "Some Title"} do %>
<p>Here is some content to be rendered inside the panel</p>
<% end %>
<%= foo %>