Using a Site Layout with the Sinatra Web Framework
This post dropped May 1st, 22:04, in to Web Development
The Sinatra web framework is very elegant, yet powerful. Once installed, all you have to do is map urls coming to Ruby functions, inside of which you can write ruby code to do just about anything.
get '/' do
haml :index
endIn this exampple, it uses Sinatra's built in haml function to run the ./views/index.haml template. But you can add whatever ruby code inside the functions you want, such as "hello world," ActiveRecord or accept a form POST.
The one thing missing was the ability to have a sitewide layout, that would wrap the output from your routing. I searched the Sinatra documentation over and over, finally finding the layout function. There wasn't much there, but playing around with the code I managed to figure out how it works.
By default, Sintara will look for a ./views/layout.ext file to use as a template, where "ext" is replaced with your templating-system specific extenstion. For instance, if you're using erb it's layout.erb and if it's haml you use layout.haml. If you just return markup with your route, it will not use any template file.
Now, if you want to use a different layout than the standard one, you'll have to specify that in your template function like so.
haml :index, {:layout => :special}That's it!
Hope this helps some people trying to figure out layouts in Sinatra.
Post Details
- Published
- May 1st, 22:04
- Category
- Web Development
- Author
- Jeff
Recent Posts
-
Freshbooks - My new best friend
2 weeks, 5 days ago -
Using jQuery Inside Your Firefox Extension
2 weeks, 6 days ago -
Using a Site Layout with the Sinatra Web Framework
on 1/5/08 -
Why You Should Use Persistent Connections with MySQL
on 11/4/08 -
CakePHP Best Practices: Rethinking the hasAndBelongsToMany Association
on 27/3/08 -
CakePHP Best Practices: Fat Models and Skinny Controllers
on 23/1/08 -
10 Steps to Becoming a Professional Web Developer
on 12/1/08 -
The Difference Between Good Code and a Good App
on 10/1/08 -
Using Your rsync.net Account as a SVN Repository
on 4/1/08 -
Welcome to GLUEinteractive
on 13/11/07
