Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Nested layouts (See related posts)

I was missing some feature in Rails - nested layouts. So I took some time and implemented it:

module ApplicationHelper
  def inside_layout(layout, &block)
    @template.instance_variable_set("@content_for_layout", capture(&block))
    
    layout = layout.include?("/") ? layout : "layouts/#{layout}" if layout
    buffer = eval("_erbout", block.binding)
    buffer.concat(@template.render_file(layout, true))
  end
end


Then, design your views to use e.g. 'inner_layout'.

In 'layouts/inner_layout.rhtml' write:

<% inside_layout 'outer_layout' do %>

 layout stuff
  <%= @content_for_layout %>

<% end %>


The 'outer_layout.rhtml' will go as ussual layout (unless it needs to be nested in other higher level layout)

Comments on this post

silvestre posts on Sep 20, 2007 at 09:11
Awesome! Thanks!
This should really be in Rails core. :-)

You need to create an account or log in to post comments to this site.


Click here to browse all 5140 code snippets

Related Posts