Rendering different layouts with Sinatra
I had a project recently that had a requirement of a frontend, and an admin backend. Pretty normal for an application, but of course the admin had a different layout, set of tabs, etc. I didn’t want to say :layout => ‘admin’ everywhere, so I came up with this (very small) code snippet to make rendering a different layout easy.
def admin_haml(template, options={}) haml(template, options.merge(:layout => :'admin/layout')) end
Which I then used in my code like:
get '/admin/users' do @users = User.all admin_haml :'admin/user/index' end
This also shows how to have nested directories in the views folder if you were curious. This will look for the view views/admin/users/index.haml, and render it with the layout views/admin/layout.haml.
March 31st, 2011 at 5:45 am
or just:
set :haml, :layout => :’admin/layout’ to set a default layout for your sinatra module