Module: Mustache::Sinatra

Defined in:
lib/mustache/sinatra.rb

Overview

Support for Mustache in your Sinatra app.

require 'mustache/sinatra'

class Hurl < Sinatra::Base
  register Mustache::Sinatra

  set :mustache, {
    # Should be the path to your .mustache template files.
    :templates => "path/to/mustache/templates",

    # Should be the path to your .rb Mustache view files.
    :views => "path/to/mustache/views",

    # This tells Mustache where to look for the Views module,
    # under which your View classes should live. By default it's
    # the class of your app - in this case `Hurl`. That is, for an :index
    # view Mustache will expect Hurl::Views::Index by default.
    # If our Sinatra::Base subclass was instead Hurl::App,
    # we'd want to do `set :namespace, Hurl::App`
    :namespace => Hurl
  }

  get '/stats' do
    mustache :stats
  end
end

As noted above, Mustache will look for ‘Hurl::Views::Index` when `mustache :index` is called.

If no ‘Views::Stats` class exists Mustache will render the template file directly.

You can indeed use layouts with this library. Where you’d normally <%= yield %> you instead {{yield}} - the body of the subview is set to the ‘yield` variable and made available to you.

Defined Under Namespace

Modules: Helpers

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object

Called when you ‘register Mustache::Sinatra` in your Sinatra app.



150
151
152
# File 'lib/mustache/sinatra.rb', line 150

def self.registered(app)
  app.helpers Mustache::Sinatra::Helpers
end