Geeks With Blogs
Liam McLennan

sinatra Sinatra is a Ruby DSL for building web applications. It is distinguished from its peers by its minimalism. Here is hello world in Sinatra:

require 'rubygems'
require 'sinatra'
get '/hi' do
  "Hello World!"

A haml view is rendered by:

def '/'
  haml :name_of_your_view

Haml is also new to me. It is a ruby-based view engine that uses significant white space to avoid having to close tags. A hello world web page in haml might look like:

		%title Hello World
		%div Hello World

You see how the structure is communicated using indentation instead of opening and closing tags. It makes views more concise and easier to read.

Based on my syntax highlighter for Gherkin I have started to build a sinatra web application that publishes syntax highlighted gherkin feature files. I have found that there is a need to have features online so that customers can access them, and so that they can be linked to project management tools like Jira, Mingle, trac etc.

The first thing I want my application to be able to do is display a list of the features that it knows about. This will happen when a user requests the root of the application. Here is my sinatra handler:

get '/' do
  feature_service =,
  @features = feature_service.features(settings.feature_path, settings.feature_extensions)
  haml :index

The handler and the view are in the same scope so the @features variable will be available in the view. This is the same way that rails passes data between actions and views. The view to render the result is:

%h2 Features
  - @features.each do |feature|
      %a{:href => "/feature/#{}"}=

Clearly this is not a complete web page. I am using a layout to provide the basic html page structure. This view renders an <li> for each feature, with a link to /feature/#{}. Here is what the page looks like:


When the user clicks on one of the links I want to display the contents of that feature file. The required handler is:

get '/feature/:feature' do
  @feature_name = params[:feature]
  feature_service =,
  # TODO replace with feature_service.feature(name)
  @feature = feature_service.features(settings.feature_path, settings.feature_extensions).find do |feature| == @feature_name
  haml :feature

and the view:

%pre{:class => "brush: gherkin"}= @feature.description

%div= partial :_back_to_index

%script{:type => "text/javascript", :src => "/scripts/shCore.js"}
%script{:type => "text/javascript", :src => "/scripts/shBrushGherkin.js"}
%script{:type => "text/javascript" } SyntaxHighlighter.all();

Now when I click on the Search link I get a nicely formatted feature file:


If you would like see the full source it is available on bitbucket.

Posted on Thursday, April 1, 2010 12:36 AM | Back to top

Copyright © Liam McLennan | Powered by: