Nested layouts in Rails 3
Published 5 months ago

This an update/rewrite of my previous post nested layouts – as that version no longer works with Rails 3.

Code

def parent_layout(layout)
  @_content_for[:layout] = self.output_buffer
  self.output_buffer = render(:file => "layouts/#{layout}")
end

Add the above helper method in your ApplicationHelper or wherever you see fit.

Example

Let’s say your application has an admin section. And for all the views using admin.html.erb layout, you want to fetch stylesheets, javascript, footer etc. from the application.html.erb layout. One way to do this would be to keep the shared code in partials. However, using inheritance style layouts feels more natural to me.

Here’s how you can use application.html.erb as the layout for the admin.html.erb layout :

# layouts/admin.html.erb
<h1>Admin Panel</h1>
<div class="admin_navigation">Home | Posts | Users | Assets</div>
<%= yield %>

<%= parent_layout 'application' %>

Your application.html.erb layout is just another regular layout that can be used independently.

# layouts/application.html.erb
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head><title>Hello World</title></head>
  <body>
    <%= yield %>
  </body>
</html>

Using admin.html.erb layout is now affectively same as using the following layout :

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head><title>Hello World</title></head>
  <body>
    <h1>Admin Panel</h1>
    <div class="admin_navigation">Home | Posts | Users | Assets</div>
    <%= yield %>
  </body>
</html>

Glad you liked it. Would you like to share?

Sharing this page …

Thanks! Close

Add New Comment

Showing 7 comments

Sort by   Subscribe by email   Subscribe by RSS
  • I've been having trouble implementing this when using HAML templates. The HAML implementation seems to override #output_buffer in ways that don't seem to work here. The code in the parent layout is definitely being parsed – a couple of logger.info calls confirm that - but the only output is that from the child layout.

    The project I'm currently working on would greatly benefit from this approach, as it'd greatly simplify some of our existing code. Unfortunately, I don't know enough about how Rails 3's templating engine works to yet work out why the approach above works with ERb and not HAML...
  • Thanks for the masterpiece!
  • freemba 5 months ago
    Good to see a new post ;)
  • schlick 5 months ago
    Here's another way of doing nested layouts (from the Rails Guides): http://guides.rubyonrails.org/...
  • I've yet to use this technique, but I noticed #render now takes a block so you could simulate nested layouts by: render(:layout => 'layouts/base') { ... }

    https://gist.github.com/740835
  • This always happen from time to time on Rails. I've written about nested layouts in the template guides in the past. After then, you posted about the same subject (at that time, my solution also didn't work anymore). Now you are posting again another solution for the same problem. Isn't this a common request that should be integrated in the Rails API so that it is maintained among Rails versions as well as its documentation?
  • Huh, interesting. Are these internals (like self.output_buffer, or the [special? magic?] :layout item in @_content_for) documented somewhere, or is this the point where one should dig into the source?

    I don't suppose there is some architectural overview of ActionView somewhere (beyond the basic guide on rubyonrails.org), is there?

Reactions

  • progressions 4 months ago
      From  twitter
    Nested layouts in Rails 3 http://bit.ly/f8PBRD
  • linuxonrails 5 months ago
      From  twitter
    RT @planetonrails: Nested layouts in Rails 3 http://bit.ly/hWFlDE
  • derekcroft 5 months ago
      From  twitter
    RT @StanBright: Nested layouts in Rails 3 http://m.onkey.org/nested-layouts-in-rails-3
  • StanBright 5 months ago
      From  twitter
    Nested layouts in Rails 3 http://m.onkey.org/nested-layouts-in-rails-3
  • knick44 5 months ago
      From  twitter
    @patientslikeme can we have nice things? RT @planetonrails: Nested layouts in Rails 3 http://bit.ly/hWFlDE
  • planetonrails 5 months ago
      From  twitter
    Nested layouts in Rails 3 http://bit.ly/hWFlDE
  • rssthingies 5 months ago
      From  twitter
    Pratik: Nested layouts in Rails 3: This an update/rewrite of my previous post nested layouts – as that version n... http://bit.ly/eXpG2k