Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nesting Layouts in Sinatra

Tags:

ruby

erb

sinatra

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.

like image 526
Pat Newell Avatar asked Jun 20 '13 14:06

Pat Newell


1 Answers

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!

like image 55
Pat Newell Avatar answered Oct 14 '22 10:10

Pat Newell