Rails 使用块呈现部分内容

我正在尝试重用我编写的提供面板样式的 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。

63614 次浏览

我认为它将工作(只是做了快速脏测试) ,如果你分配给一个变量,然后输出它。

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

虽然上面的两个答案都很有用(至少托尼链接到了这个例子) ,但我最终还是在上面的帖子中找到了最简洁的答案(评论来自科内利斯 · 西茨马)

我想 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>

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

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

这里有一个基于以前答案的替代方案。

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

基于已被接受的答案,这就是我使用 Rails 4的成功之处。

我们可以呈现这样一个面板:

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

enter image description here

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

Helper 方法(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