module Mustache::Sinatra::Helpers
def mustache(template, options={}, locals={})
def mustache(template, options={}, locals={}) render :mustache, template, options, locals end
def render_mustache(template, data, opts, locals, &block)
This is called by Sinatra's `render` with the proper paths
def render_mustache(template, data, opts, locals, &block) # If you have Hurl::App::Views, namespace should be set to Hurl::App. Mustache.view_namespace = options.namespace # This is probably the same as options.views, but we'll set it anyway. # It's used to tell Mustache where to look for view classes. Mustache.view_path = options.mustaches # Grab the class! klass = Mustache.view_class(template) # Only cache the data if this isn't the generic base class. klass.template = data unless klass == Mustache # Confusingly Sinatra's `views` setting tells Mustache where the # templates are found. It's fine, blame Chris. if klass.template_path != options.views klass.template_path = options.views end # Create a new instance for playing with instance = klass.new # Copy instance variables set in Sinatra to the view instance_variables.each do |name| instance.instance_variable_set(name, instance_variable_get(name)) end # Locals get added to the view's context locals.each do |local, value| instance[local] = value end # If we're paseed a block it's a subview. Sticking it in yield # lets us use {{yield}} in layout.html to render the actual page. instance[:yield] = block.call if block instance.template = data unless instance.compiled? instance.to_html end