Introduction to Rango - Jakub Šťastný
Rango is a lightweight web framework, based on Rack. The goal is to provide a solid framework for building sites in Ruby; bigger than Sinatra but smaller than Rails or Merb.
Here's a quick summary. Rango:
- Requires Ruby 1.9.
- Provides code generators, but doesn't insist that you use them.
- Reloads your code in development (via shotgun).
- supports bundler (through a generator).
It uses one of Django's naming conventions; the Rango equivalent of a Rails view is known as a template, while a Rails controller action is called a view.
There are various routers supported (Usher, rack-mount, URLMap, rack-router, etc.). You also have a Rango::UrlHelper module that provides a url()
function for generating URLs.
Jakub talked us through how rendering works, with some example code for a controller:
require "rango/mixins/render"
class Posts < Rango::Controller
include Rango::RenderMixin
def show
post = Post.get(params[:id])
render "post.html", post: post
end
end
You can then render your template like this:
%h1= post.title
Alternatively you can use explicit rendering (the template looks the same as in the previous example):
class Posts < Rango::Controller
include Rango::ExplicitRendering
def show
context[:post] = Post.get(params[:id])
render "post.html"
end
end
Or implicit rendering (this'll be more familiar to Rails developers):
class Posts < Rango::Controller
include Rango::ImplicitRendering
def show
@post = Post.get(params[:id])
render "post.html"
end
end
And the template:
%h1= @post.title
Template inheritance
Rango uses template inheritance (an idea borrowed from Django, though to be fair Django didn't invent it) is arguably a more flexible approach than layout/view approach used by Rails and Merb.
To use the templates you define named blocks that return HTML. The blocks are executed in the templates at the top of the inheritance hierarchy, and optionally overridden in templates further down the hierarchy:
/ base.html.haml
%html
%head
= js *["app.js", *block(:js)]
%title= block(:title)
= block(:head)
%body
%h1= block(:title)
= block(:content)
/ index.html.haml
- extends "base.html" unless request.ajax?
- block(:js, "jquery.js", "syntax.js")
- block(:title, "Hello World!")
- block(:content) do
Lorem ipsum
Rango also supports generic views and deferred routes.
It looks like a rather nice framework; well worth checking out if you like playing around with new web frameworks. I suspect it's also a good way for a Ruby programmer to find out what's so good about Django.
What's coming up for Rango? In January Jakub will be integrating Pancake, allowing a Rango app to be run standalone or to be used as a library.
All the examples that Jakub covered are available in the ruby-manor-rango repository on his github account. He's also put his slides on slideshare.
Jakub is the lead developer at Headshift. You can find him on Twitter at @botanicus. Follow Rango's progress at http://botanicus.github.com/rango.
More talks from Ruby Manor
- Browser Level Testing - Martin Kleppmann
- Data Visualisation with Ruby - Chris Lowis
- Denormalising Your Rails Application - Dan Lucraft
- gem that - James Adam
- Ruby and Cocoa - James Mead
- RubyGoLightly - Eleanor McHugh
- Secrets of the Standard Library - Paul Battley
- Short Order Ruby - Ben Griffiths
- Testing Third Party HTTP APIs - Richard Livsey and Bartosz Blimke
- The Joy of Painting with Ruby - Jason Cale
- Ruby Manor 2008 morning session and afternoon session