mustache 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/mustache/sinatra.rb +42 -7
- data/lib/mustache/version.rb +1 -1
- metadata +1 -1
data/lib/mustache/sinatra.rb
CHANGED
|
@@ -7,15 +7,27 @@ class Mustache
|
|
|
7
7
|
# require 'mustache/sinatra'
|
|
8
8
|
#
|
|
9
9
|
# class App < Sinatra::Base
|
|
10
|
-
#
|
|
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
|
-
#
|
|
18
|
-
#
|
|
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
|
-
|
|
38
|
-
|
|
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
|
-
|
|
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
|
data/lib/mustache/version.rb
CHANGED