You pass variables to partial parts of Sinatra haml as follows:
page.haml
!!! %html{:lang => 'eng'} %body = haml :'_header', :locals => {:title => "BOOM!"}
_header.haml
%head %meta{:charset => 'utf-8'} %title= locals[:title]
In the case of the page name, I just do something like this in the btw layout:
layout.haml
%title= @title || 'hardcoded title default'
Then set the @title value in the routes (with an assistant so that it is short).
But if your title is partial, you can combine two examples, for example:
layout.haml
!!! %html{:lang => 'eng'} %body = haml :'_header', :locals => {:title => @title}
_header.haml
%head %meta{:charset => 'utf-8'} %title= locals[:title]
app.rb
helpers do def title(str = nil) # helper for formatting your title string if str str + ' | Site' else 'Site' end end end get '/somepage/:thing' do # declare it in a route @title = title(params[:thing]) end
robomc
source share