Ruby-on-rails Rails 使用块渲染部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2951105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:43:43  来源:igfitidea点击:

Rails render partial with block

ruby-on-railsrenderpartialblock

提问by brad

I'm trying to re-use an html component that i've written that provides panel styling. Something like:

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

So I see that render takes a block. I figured then I could do something like this:

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

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

And I want to do something like:

我想做类似的事情:

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

Unfortunately this doesn't work with this error:

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

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

So it doesn't like the =obviously with a block, but if I remove it, then it just doesn't output anything.

所以它=显然不喜欢带有块的,但是如果我删除它,那么它就不会输出任何东西。

Does anyone know how to do what I'm trying to achieve here? I'd like to re-use this panel html in many places on my site.

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

回答by brad

While both of those answers above work (well the example that tony links to anyway) I ended up finding the most succinct answer in that above post (comment by Kornelis Sietsma)

虽然上面的两个答案都有效(以及 tony 链接到的例子),但我最终在上面的帖子中找到了最简洁的答案(Kornelis Sietsma 的评论)

I guess render :layoutdoes exactlywhat I was looking for:

我想render :layout正是我想要的:

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

combined with:

结合:

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

回答by rebagliatte

Here's an alternative based on previous answers.

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

Create your partial on shared/_modal.html.erb:

创建您的部分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>

Define your method on application_helper.rb:

定义您的方法application_helper.rb

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

Call it from any view:

从任何视图调用它:

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

回答by Yetanotherjosh

You can use the capture helper, and even inline in the render call :

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

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

and in shared/panel:

并在共享/面板中:

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

which will produce:

这将产生:

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

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

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

回答by Kris

Based on the accepted answer this is what worked well for me using Rails 4.

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

We can render a panel as such:

我们可以这样渲染面板:

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

enter image description here

在此处输入图片说明

This requires a helper method and a shared view:

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

helper method (ui_helper.rb)

辅助方法(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

View (/ui/panel.html.haml)

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

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

回答by Tesserex

I think it will work (just did quick dirty test) if you assign it to a variable first and then output it.

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

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