Rails使用块进行部分渲染

bra*_*rad 113 ruby-on-rails render block partial

我正在尝试重新使用我编写的提供面板样式的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>
Run Code Online (Sandbox Code Playgroud)

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

# /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>
Run Code Online (Sandbox Code Playgroud)

我想做的事情如下:

#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 %>
Run Code Online (Sandbox Code Playgroud)

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

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 -%>
...
Run Code Online (Sandbox Code Playgroud)

所以它不喜欢=明显的块,但如果我删除它,那么它只是不输出任何东西.

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

bra*_*rad 197

虽然这些答案都在上面工作(很好的是tony链接到的例子)我最终在上面的帖子中找到了最简洁的答案(Kornelis Sietsma的评论)

我想render :layout正是我所寻找的:

# Some View
<%= render :layout => '/shared/panel', :locals => {:title => 'some title'} do %>
  <p>Here is some content</p>
<% end %>
Run Code Online (Sandbox Code Playgroud)

结合:

# /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>
Run Code Online (Sandbox Code Playgroud)

  • 在 Rails 5.0 中,有一个[更改](https://edgeguides.rubyonrails.org/5_0_release_notes.html#action-view-notable-changes),因此这对所有部分都是通用的,而不仅仅是布局。您可以将调用代码的第一行更改为:`&lt;%= render '/shared/panel', title: 'some title' do %&gt;` (7认同)
  • 在Rails 3.2.2中,我认为你必须使用`<%=%>`而不是`<%%>`,例如`<%= render:layout =>'/ shared/panel',` (6认同)

reb*_*tte 24

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

创建你的部分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>
Run Code Online (Sandbox Code Playgroud)

定义您的方法application_helper.rb:

def modal_for(heading, &block)
  render(
    partial: 'shared/modal',
    locals: { heading: heading, block: block }
  )
end
Run Code Online (Sandbox Code Playgroud)

从任何视图调用它:

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


Yet*_*osh 9

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

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

在共享/面板中:

<h3><%= title %></h3>
<div class="my-outer-wrapper">
  <%= captured %>
</div>
Run Code Online (Sandbox Code Playgroud)

这将产生:

<h3>Some Title</h3>
<div class="my-outer-wrapper">
  <p>Here is some content to be rendered inside the partial</p>
</div>
Run Code Online (Sandbox Code Playgroud)

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

  • 内联模式对于需要多个块的部分来说非常有用。 (2认同)

Kri*_*ris 5

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

我们可以这样渲染面板:

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

在此处输入图片说明

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

辅助方法(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
Run Code Online (Sandbox Code Playgroud)

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

.panel{ class: options[:panel_classes] }
  .panel-heading= heading
  .panel-body
    = yield
Run Code Online (Sandbox Code Playgroud)