All file-based views are looked up in:
root
| - views/
get '/' do
haml :index
end
This will render ./views/index.haml
get '/' do
sass :styles
end
This will render ./views/styles.sass
get '/' do
erb :index
end
This will render ./views/index.erb
get '/' do
builder :index
end
This will render ./views/index.builder
get '/' do
builder do |xml|
xml.node do
xml.subnode "Inner text"
end
end
end
This will render the xml inline, directly from the handler.
Assume that your site url is http://liftoff.msfc.nasa.gov/.
get '/rss.xml' do
builder do |xml|
xml.instruct! :xml, :version => '1.0'
xml.rss :version => "2.0" do
xml.channel do
xml.title "Liftoff News"
xml.description "Liftoff to Space Exploration."
xml.link "http://liftoff.msfc.nasa.gov/"
@posts.each do |post|
xml.item do
xml.title post.title
xml.link "http://liftoff.msfc.nasa.gov/posts/#{post.id}"
xml.description post.body
xml.pubDate Time.parse(post.created_at.to_s).rfc822()
xml.guid "http://liftoff.msfc.nasa.gov/posts/#{post.id}"
end
end
end
end
end
end
This will render the rss inline, directly from the handler.
Layouts are simple in Sinatra. Put a file in your views directory named "layout.erb", "layout.haml", or "layout.builder". When you render a page, the appropriate layout will be grabbed (of the same filetype), and used.
The layout itself should call yield
at the point you want the content to be
included.
An example haml layout file could look something like this:
%html
%head
%title SINATRA BOOK
%body
#container
= yield
Sometimes you don't want the layout rendered. In your render method just pass :layout => false, and you're good.
get '/' do
haml :index, :layout => false
end
This one is cool:
get '/' do
haml :index
end
use_in_file_templates!
__END__
@@ layout
X
= yield
X
@@ index
%div.title Hello world!!!!!
Try it!