简体   繁体   中英

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. Why?

Pass the block itsef instead of its return value to content_tag:

content_tag(:div, class: 'panel-body', &block)

Other method:

content_tag(:div, capture(&block), class: 'panel-body')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM