Twitter Updates

Friday, 5 February 2010

Sinatra layouts vs templates

I must have missed this on my first pass of the Sinatra (sinatrarb) book. There is a very nice way to create a site template, called a layout.

Just create a file in views called layout.erb (or layout.haml if using haml) output yield where you want the body of the templates to be inserted. Class @variables can also be used.
This allows the basic layout of your site Headers, Footers and navigation panes to be controlled independently, a change in layout.erb is instantly applied across your whole site.

layout.erb
< h t m l>
< h e a d>
< t i t l e><%= @title %>< / t i t l e>
< / h e a d>
< b o d y>
<%= yield %>
< / b o d y>
< / h t m l>


you app.rb (NB: no refference to layour.erb is made)
get '/about/?' do
@title = "About"
erb :about
end


/views/about.erb
<p>Example about page</p>

1 comment:

Anonymous said...

Thank. you! :)