Templating engines

All the template formats supported by Sinatra are supported by Nesta, including Haml, Erb, Erubis, Liquid, Sass, Scss and Less. You can even write your JavaScript in CoffeeScript if you like, and Nesta will render it for you. See Sinatra’s template docs for the full list.

To use a different template engine you’ll need to modify the routes, updating them so that they call the relevant Sinatra method to render the output.

Copy the default routes out of the gem’s lib/nesta/app.rb file and paste them into an app.rb file in your project’s folder. You can find the path to the gem’s files with this command:

$ bundle show nesta

When you paste the routes into your app.rb file you need to make sure that you’re adding them to the Nesta::App class. Something like this will do the job:

$ cd mysite.com
$ cat app.rb
module Nesta
  class App
    get '/css/:sheet.css' do
      content_type 'text/css', :charset => 'utf-8'
      cache sass(params[:sheet].to_sym)
    end

    # There are more routes than this,
    # but you get the idea...
    get '*' do
      # stuff
      cache haml(@page.template, :layout => @page.layout)
    end
  end
end

 An example: Using Erb

To use Erb instead of Haml you would then change the call to the haml method on the last line of the '*' route and change it so that it said:

cache erb(@page.template, :layout => @page.layout)

To find out which methods to call for your preferred template engine, see the Sinatra template docs.

Using the SCSS format for Sass files

Nesta’s default template uses the original Sass syntax, which uses indentation rather than CSS’s curly braces. Here’s an example:

body
  width: 92%
  margin: 0 auto

If you’d rather write plain CSS (but still benefit from the power of Sass’s variables, mixins and functions) you can use the SCSS format which is a superset of CSS (in other words, it looks identical but adds lots of juicy features if you’d like to use it). Just create a .scss file and get going.

If you’d like to base your design on the built in template (or a theme) that uses the .sass format you can convert it with the sass-convert script (which comes with Sass):

$ sass-convert path/to/mystyles.sass path/to/mystyles.scss

Don’t forget to move the .sass file out of the way so that Nesta doesn’t find it and use it in preference to your new .scss file.