# File lib/mustache/sinatra.rb, line 45
      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

        # If they aren't explicitly disabling layouts, try to find
        # one.
        if options[:layout] != false
          # Let the user pass in a layout name.
          layout_name = options[:layout]

          # If all they said was `true` (or nothing), default to :layout.
          layout_name = :layout if layout_name == true || !layout_name

          # If they passed a layout name use that.
          layout = mustache_class(layout_name, options)

          # If it's just an anonymous subclass then don't bother, otherwise
          # give us a layout instance.
          if layout.name && layout.name.empty?
            layout = nil
          else
            layout = layout.new
          end
        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)

        # 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

        # 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