[英]How can I yield more than one partial in a block?
I asked a similar question recently, though that was aimed at having multiple blocks as arguments. 我最近问了一个类似的问题,尽管那是为了让多个块作为参数。 This problem is a little more immediate. 这个问题更加直接。
The problem I have is, I have a helper method which I want to be able to pass content as a block to render. 我遇到的问题是,我有一个帮助方法,我希望能够将内容作为一个块传递给渲染。 However, if I add more than one partial only the last one in the block is rendered. 但是,如果我添加多个部分,则仅渲染块中的最后一个。 The methods are below. 方法如下。
def bootrap_panel(title, klass = 'primary', &block)
content_tag(:div, panel_heading(title) + panel_body(&block), class: 'panel panel-' + klass)
end
def panel_body(&block)
content_tag(:div, yield, class: 'panel-body') if block_given?
end
And an example of an issue I am having is here, where only the last partial is being displayed on the page. 我遇到的一个问题就是这里,只有最后一个部分显示在页面上。
=bootrap_panel 'Panels', 'primary' do
- render "dynamic_panels/partials/new"
- render "dynamic_panels/partials/dynamic_panels", dynamic_panels: dynamic_page.dynamic_panels
My first thought is, well I should be yielding the partials to an array or something first, then display that (I am not sure I am able to do that). 我的第一个想法是,我应该首先让部分数组或类似的东西,然后显示(我不确定我能够做到这一点)。 Secondly, i am using the '-' operator instead of the '=' operator for displaying ruby content in haml. 其次,我使用' - '运算符而不是'='运算符来显示haml中的ruby内容。 Why? 为什么?
Pass the block itsef instead of its return value to content_tag: 将块itsef而不是其返回值传递给content_tag:
content_tag(:div, class: 'panel-body', &block)
Other method: 其他方法:
content_tag(:div, capture(&block), class: 'panel-body')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.