Sinatra provides different perspective and workflow to Web development with a tiny and elegent domain specific language for HTTP(s) actions. Although no strict MVC pattern enforced, Sinatra supports many template languages for its dynamic views. Among these template languages, Haml and Slim are popular choices because their succinct Ruby-like syntaxes. However, I prefer ERB over other newer template languages like Haml or Slim because the syntax of ERB follows original HTML tags, meaning better recognition by editor and web designer.
Erb uses Ruby itself as the template language, meaning that there is no need to re-learn another template language. In Erb,
<%# %> stands for comments,
<%= %> for text substitutions, and
<% %> for non-text Ruby code. For example, if we want to loop over an array in out template, just use
for loop or
each method to iterate over array elements.
Here we use
if clause to optionally include meta tag.
If you want to use Erb in Sinatra, use
erb method in Sinatra file.
In the above example, we use
index.erb as template, pass
title as variable in template, and use
main_layout.erb as layout. You may also pass class variable to template directly without setting
In this example, we still use
index.erb as template.
@title will pass to template, you don’t need to explictly setting local variables.
Although Erb doesn’t support template inheritance, you can use layout to mimic the effect and reduce repeated code. Layout is simply template with
<%= yield => block. After finishing layout template, you just need to fill content in your main template. By default, layout template will be named as
You may also use different layouts in different pages. Nevertheless, these layouts may have repeated region among them. You need partial to further reduce these parts.
Partial is supported in Sinatra. To utilitize partial, use
erb method in template.
Then, design your head.erb.
You may also design your own header.erb and footer.erb in the same manner.
<%= yield %> should be sufficient for most conditions. However, if you need fine control over template blocks, you may consider
content_for is not a built-in part of Sinatra, but you can easily get it from sinatra-contrib gem. The main point of
content_for is that you can use several blocks in template instead of only one
If you want to use
content_for, require it in main Sinatra file:
Then, in your main template:
Erb keeps original HTML tags, making a easier life for web designers. By combining layout, partial, and content_for, you can reduce repeated tags and maintain good templates.