How to make a layout template in Sinatra?

maček picture maček · Nov 16, 2010 · Viewed 14.8k times · Source

I'm new to Sinatra and I can't figure out where to put my application layout.

I've seen the inline method that uses

# app code    
__END__

@@layout
  %html
    = yield

But I'd like the layout to be in it's own .haml file.

What should the layout file be named? What directory should it be placed in?

Answer

Phrogz picture Phrogz · Nov 17, 2010

Automatic Wrapping

To make every view default to be wrapped in a layout, create a file in views/layout.haml and your calls to haml :myview will automatically be wrapped in this layout.

Skipping the Layout

If you want a particular view rendering not to use the layout, use:

get '/' do
   # Other pages will use layout.haml, but not the main page
   haml :home, :layout => false
end

Using a Different Layout

If you want to use a layout other than the common layout, create another file (for example views/admin_layout.haml) and then pass this as an option:

get '/admin/create' do
   haml :create, :layout => :admin_layout
end