mustache 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,15 +7,27 @@ class Mustache
7
7
  # require 'mustache/sinatra'
8
8
  #
9
9
  # class App < Sinatra::Base
10
- # helpers Mustache::Sinatra
10
+ # # Should be the path to your .mustache template files.
11
+ # set :views, "path/to/mustache/templates"
12
+ #
13
+ # # Should be the path to your .rb Mustache view files.
14
+ # # Only needed if different from the `views` setting
15
+ # set :mustaches, "path/to/mustache/views"
16
+ #
17
+ # # This tells Mustache where to look for the Views modules,
18
+ # # under which your View classes should live. By default it's
19
+ # # Object. That is, for an :index view Mustache will expect
20
+ # # Views::Index. In this example, we're telling Mustache to look
21
+ # # for index at App::Views::Index.
22
+ # set :namespace, App
11
23
  #
12
24
  # get '/stats' do
13
25
  # mustache :stats
14
26
  # end
15
27
  # end
16
28
  #
17
- # If a `Views::Stats` class exists in the above example,
18
- # Mustache will try to instantiate and use it for the rendering.
29
+ # As noted above, Mustache will look for `App::Views::Index` when
30
+ # `mustache :index` is called.
19
31
  #
20
32
  # If no `Views::Stats` class exists Mustache will render the template
21
33
  # file directly.
@@ -34,16 +46,37 @@ class Mustache
34
46
  def render_mustache(template, data, options, locals, &block)
35
47
  name = Mustache.classify(template.to_s)
36
48
 
37
- if defined?(Views) && Views.const_defined?(name)
38
- instance = Views.const_get(name).new
49
+ # This is a horrible hack but we need it to know under which namespace
50
+ # Views is located. If you have Haystack::Views, namespace should be
51
+ # set to Haystack.
52
+ namespace = self.class.namespace
53
+
54
+ if namespace.const_defined?(:Views) && namespace::Views.const_defined?(name)
55
+ # First try to find the existing view,
56
+ # e.g. Haystack::Views::Index
57
+ klass = namespace::Views.const_get(name)
58
+
59
+ elsif File.exists?(file = "#{self.class.mustaches}/#{template}.rb")
60
+ # Couldn't find it - try to require the file if it exists, then
61
+ # load in the view.
62
+ require "#{file}".chomp('.rb')
63
+ klass = namespace::Views.const_get(name)
64
+
39
65
  else
40
- instance = Mustache.new
66
+ # Still nothing. Use the stache.
67
+ klass = Mustache
68
+
41
69
  end
42
70
 
71
+ # Create a new instance for playing with
72
+ instance = klass.new
73
+
74
+ # Copy instance variables set in Sinatra to the view
43
75
  instance_variables.each do |name|
44
76
  instance.instance_variable_set(name, instance_variable_get(name))
45
77
  end
46
78
 
79
+ # Locals get added to the view's context
47
80
  locals.each do |local, value|
48
81
  instance[local] = value
49
82
  end
@@ -58,4 +91,6 @@ class Mustache
58
91
  end
59
92
  end
60
93
 
61
- Sinatra.helpers Mustache::Sinatra
94
+ Sinatra::Base.helpers Mustache::Sinatra
95
+ Sinatra::Base.set :mustaches, Sinatra::Base.views
96
+ Sinatra::Base.set :namespace, Object
@@ -1,3 +1,3 @@
1
1
  class Mustache
2
- Version = '0.1.2'
2
+ Version = '0.1.3'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mustache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Wanstrath