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:

http://www.sinatrarb.com/extensions.html <– details for registering an extension
http://www.evernote.com/l/AC6vnhVWbXdLvbWD5VMbeYI8UVis3vT9mu0/ <– 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 config.ru 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)

otherwise:

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

Array of Hashes in Ruby