Skip to content
nesquena edited this page Sep 13, 2010 · 5 revisions

Introduction

This component provides a number of rendering helpers for sinatra, making the process of displaying templates far smoother. This plugin also has support for useful additions such as partials (with support for :collection) into the templating system.

Setup

# app.rb
require 'sinatra/base'
require 'sinatra_more'

class Application < Sinatra::Base
  # ...
  register SinatraMore::RenderPlugin
 # ...
end

Template Rendering

Using render plugin helpers is extremely simple. If you want to render an erb template in your view path:

 erb_template 'path/to/my/template'

or using haml templates works just as well:

haml_template 'path/to/haml/template'

There is also a method which renders the first view matching the path and removes the need to explicitly define an engine:

 render_template 'path/to/any/template'

It is worth noting these are mostly for convenience. When I had more complex view files in sinatra, I got tired of writing:

haml :"the/path/to/file"
erb "/path/to/file".to_sym

Template Partials

This plugin provides partials support for rendering mini-templates onto directly a page:

partial 'photo/_item', :object => @photo, :locals => { :foo => 'bar' }
partial 'photo/_item', :collection => @photos

This works as you would expect and also supports the collection counter inside the partial item_counter

 # /views/photo/_item.haml
# Access to collection counter with <partial_name>_counter i.e item_counter
# Access the object with the partial_name i.e item
Clone this wiki locally