helpers and extensions for Sinatra

These are some quick notes (mostly for self-reference) on how to implement helpers and extensions in Sinatra (for extensions, the links to sinatra’s website are below). So, helpers straight from the book “Sinatra: Up and Running (pg. 75+)“.



Starting out with a basic skeleton for an ApplicationController:

But where’s title coming from? the Application helper in the full path of views:

now, in views/layouts.erb:

That sets the default layout for any part of your app.

As a side note: in the book I’m excerpting this from (Sinatra: Up and Running), the author made what, ostensibly is a mistake…

Probably NOT a mistake, though, considering the structure of Ruby; I can only speculate why a class instance variable such as @title would be translated into “title” as with that piece of erubis code. The solution that should be the real way things work (and also reduces confusion) is:

For registering an extension, I’ll just leave these here as links: <– details for registering an extension <– for backup/offline purposes

Moving on… Now we need a controller that that will get a page

And now for a view which corresponds to ‘erb :example’.

In views/example.erb:

How to run it? siiiigh, need for this setup:

That about does wraps things up. left out not_found, so, todo: add not_found.

to start up sinatra you can do something like

thin -C configure.yml <– generates a default configuration file, but if you’re on windows -C won’t work (see previous post)


You may want to have a separate config-*.yml file for production and development.

Default layouts in Sinatra (except from a stackxchange post)

Automatic Wrapping

To make every view default to be wrapped in a layout, create a file in erb/layout.erb and your calls to erb :myview will automatically be wrapped in this layout.

Skipping the Layout

If you want a particular view rendering not to use the layout, use:


Using a Different Layout

If you want to use a layout other than the common layout, create another file (for example views/admin_layout.haml) and then pass this as an option:


Quick Coding: dealing with quirks in Sinatra + Thin + Windows 8.1 (part 1)

This is mostly to document an ongoing issue I’ve been going with while tinkering around with Sinatra in Windows 8.1, sadly.



A while back I never documented issues passing through arguments with Sinatra while using it with a combination of, thin, and rackup on the command line. Well, I finally found a one-liner that makes life with sinatra/windows 8.1 SLIGHTLY easier… First though:

2014-09-21 22_19_39-Add New Post ‹ TheInterface.rb — WordPress - Pale Moon

All I really need is:

“-e development” (etc)  sets your application to run in development or production (forgot what it defaults to), but you have to configure a few things depending on what you want to do.

Then somewhere within my app class:

In this case, I don’t have Sinatra::Reloader registered while in production mode, since I want Sinatra to be a bit more persistent instead of reloading per page request.

But the magic piece of code is

You could pretty much name your environment(s) anything.


More to come.


Update: Now i’m investigating something like this:


Well, my assumptions after using thin from the command line for a while is true: the argument to parse a configuration file (-C) doesn’t seem to be available on Windows 8.1 or at least this version of thin… That kinda sucks; I may poke around the source code for thin a while and see what kind of patch I can come up with.