module Mustache::Sinatra::Helpers

def compile_mustache(view, options = {})

appropriate view class for this view.
Given a view name and settings, finds and prepares an
def compile_mustache(view, options = {})
  options[:templates] ||= settings.views if settings.respond_to?(:views)
  options[:namespace] ||= self.class
  factory = Class.new(Mustache) do
    self.view_namespace = options[:namespace]
    self.view_path      = options[:views]
  end
  # Try to find the view class for a given view, e.g.
  # :view => Hurl::Views::Index.
  klass = factory.view_class(view)
  # If there is no view class, issue a warning and use the one
  # we just generated to cache the compiled template.
  if klass == Mustache
    warn "No view class found for #{view} in #{factory.view_path}"
    klass = factory
    # If this is a generic view class make sure we set the
    # template name as it was given. That is, an anonymous
    # subclass of Mustache won't know how to find the
    # "index.mustache" template unless we tell it to.
    klass.template_name = view.to_s
  end
  # Set the template path and return our class.
  klass.template_path = options[:templates] if options[:templates]
  klass
end

def mustache(template, options={}, locals={})

Call this in your Sinatra routes.
def mustache(template, options={}, locals={})
  # Locals can be passed as options under the :locals key.
  locals.update(options.delete(:locals) || {})
  # Grab any user-defined settings.
  if settings.respond_to?(:mustache)
    options = settings.send(:mustache).merge(options)
  end
  # Find and cache the view class we want. This ensures the
  # compiled template is cached, too - no looking up and
  # compiling templates on each page load.
  klass = mustache_class(template, options)
  # If they aren't explicitly diabling layouts, try to find
  # one.
  if options[:layout] != false
    # If they passed a layout name use that.
    layout = mustache_class(options[:layout] || :layout, options)
    # If it's just an anonymous subclass then don't bother, otherwise
    # give us a layout instance.
    if layout.name.empty?
      layout = nil
    else
      layout = layout.new
    end
    # Does the view subclass the layout? If so we'll use the
    # view to render the layout so you can override layout
    # methods in your view - tricky.
    view_subclasses_layout = klass < layout.class if layout
  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
  # Render with locals.
  rendered = instance.render(instance.template, locals)
  # Now render the layout with the view we just rendered, if we
  # need to.
  if layout && view_subclasses_layout
    rendered = instance.render(layout.template, :yield => rendered)
  elsif layout
    rendered = layout.render(layout.template, :yield => rendered)
  end
  # That's it.
  rendered
end

def mustache_class(template, options)

Returns a View class for a given template name.
def mustache_class(template, options)
  @template_cache.fetch(:mustache, template) do
    compile_mustache(template, options)
  end
end