Nested Layouts in Rails

Viewed 7506

I'm having trouble finding a way to do the following:

Let's say in my application.html.erb I have the following

<div id="one" >
  <%= yield %>
</div>

Then I want to have another layout file asdf.html.erb

<div id="two">
  <%= yield %>
</div>

I want the final output to be

<div id="one">
  <div id="two">
     <%= yield %>
  </div>
</div>

Is it possible? Thanks.

6 Answers

I have used this approach for years:

# app/helpers/layout_helper.rb
module LayoutHelper
  def inside_layout(layout = 'application', &block)
    render inline: capture(&block), layout: "layouts/#{layout}"
  end
end

Then your asdf layout would be:

# app/views/layouts/asdf.html.erb
<%= inside_layout do %>
  <div id="two">
    <%= yield %>
  </div>
<% end %>
Related