Ruby – Nesting Layouts in Sinatra

erbrubysinatra

tl;dr: is there a clean way to nest layouts in Sinatra?

For all pages on my site, I have a common layout.erb which renders a header, a footer, and some other bits.

For a subset of those pages, I would like to use an inner layout which renders a left menu in addition to those common bits.

globally

erb :pageTemplate executes layout.erb, where yield executes pageTemplate

in the subset

erb :pageTemplate executes layout.erb, where yield executes specificLayout.erb, where yield executes pageTemplate.


make sense?

I am open to separate classes, before statements, and any other ruby magic. I am not looking for adding header/footer partials and including them in each layout.

Best Answer

Found it! http://www.sinatrarb.com/intro.html#Templates%20with%20%3Ccode%3Eyield%3C/code%3E%20and%20nested%20layouts

erb :site_layout, :layout => false do
  erb :region_layout do
    erb :page
  end
end

now, :site_layout can contain the header and footer, :region_layout can contain left navigation, and :page only has to worry about the page content!