Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Sinatra: `mustache` takes a View class to skip lookups.

  • Loading branch information...
commit 6c4e12d58844d99909dfdf1133c64536cd0266af 1 parent 68ccead
@defunkt defunkt authored
Showing with 23 additions and 4 deletions.
  1. +23 −4 lib/mustache/sinatra.rb
View
27 lib/mustache/sinatra.rb
@@ -39,6 +39,18 @@ class Mustache
# 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.
+ #
+ # If you don't want the Sinatra extension to look up your view class,
+ # maybe because you've already loaded it or you're pulling it in from
+ # a gem, you can hand the `mustache` helper a Mustache subclas directly:
+ #
+ # # Assuming `class Omnigollum::Login < Mustache`
+ # get '/login' do
+ # @title = "Log In"
+ # require 'lib/omnigollum/views/login'
+ # mustache Omnigollum::Login
+ # end
+ #
module Sinatra
module Helpers
# Call this in your Sinatra routes.
@@ -72,10 +84,17 @@ def mustache(template, options={}, locals={})
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)
+ # If instead of a symbol they gave us a Mustache class,
+ # use that for rendering.
+ klass = template if template.is_a?(Class) && template < Mustache
+
+ # Find and cache the view class we want if we don't have
+ # one yet. This ensures the compiled template is cached,
+ # too - no looking up and compiling templates on each page
+ # load.
+ if klass.nil?
+ klass = mustache_class(template, options)
+ end
# Does the view subclass the layout? If so we'll use the
# view to render the layout so you can override layout
Please sign in to comment.
Something went wrong with that request. Please try again.