Rails render partial with block

前端 未结 5 1910
灰色年华
灰色年华 2020-12-04 06:35

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

  
相关标签:
5条回答
  • 2020-12-04 06:53

    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 %>
    
    0 讨论(0)
  • 2020-12-04 06:59

    Here's an alternative based on previous answers.

    Create your partial on 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:

    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 %>
    
    0 讨论(0)
  • 2020-12-04 07:04

    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)

    I guess render :layout does exactly what I was looking for:

    # 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>
    
    0 讨论(0)
  • 2020-12-04 07:13

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

    We can render a panel as such:

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

    This requires a helper method and a shared view:

    helper method (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)

    .panel{ class: options[:panel_classes] }
      .panel-heading= heading
      .panel-body
        = yield
    
    0 讨论(0)
  • 2020-12-04 07:19

    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

    0 讨论(0)
提交回复
热议问题