Sinatra 1.1 Released

The latest version of Sinatra (1.1) was released a few weeks ago. Here are a few of the notable additions:

SCSS Support

When I originally wrote about Sass, I was using the traditional syntax. since then I’ve started using the new SCSS syntax. One reason for this is it is actually a super-set of normal CSS, so it looks just the same, but has all the extra benefits of Sass (mixins, variables etc). You can also just copy plain old CSS into a SCSS file and it will work. Sinatra now supports this, all you need is a line like this:

get('/styles.css'){ content_type 'text/css', :charset => 'utf-8' ; scss :styles }

Then put a file called styles.scss in your views directory.

Nested Templates

You can now render a template within another template without having to specify :layout => false. This makes basic partials less onerous to write. For example:

%h1 Here is your order
=haml :order

This means that in a lot of cases, you won’t need a partial helper, although it may still be useful if you want some of the advanced partial features.

Pattern Matching in Before and After Filters

You can now target specific routes by adding a regular expression to a before or after filter. For example, if you want to check that somebody is logged in before they access an admin url, you could use this code:

before '/admin/*' do

You can also pass a variable to the filter, in the same way that Sinatra’s handlers work. For example, you could set an instance variable of @title based on the url of a page in your blog using the following code:

before '/blog/:title' do |slug|
  @title = title

Sinatra just keeps getting better and better. Well done to all the developers who make this possible, especially Konstantin Haase, who seems to be putting a lot of effort into improving Sinatra.

You can see the full changes on the official changelog.

blog comments powered by Disqus