mustache 0.1.3 → 0.1.4
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/README.md +9 -0
- data/Rakefile +2 -1
- data/lib/mustache/sinatra.rb +60 -50
- data/lib/mustache/version.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -304,6 +304,15 @@ Now:
|
|
304
304
|
Convoluted but you get the idea.
|
305
305
|
|
306
306
|
|
307
|
+
Sinatra
|
308
|
+
-------
|
309
|
+
|
310
|
+
Mustache ships with Sinatra integration. Please see
|
311
|
+
`lib/mustache/sinatra.rb` or
|
312
|
+
<http://defunkt.github.com/mustache/classes/Mustache/Sinatra.html> for
|
313
|
+
complete documentation.
|
314
|
+
|
315
|
+
|
307
316
|
Installation
|
308
317
|
------------
|
309
318
|
|
data/Rakefile
CHANGED
data/lib/mustache/sinatra.rb
CHANGED
@@ -6,7 +6,9 @@ class Mustache
|
|
6
6
|
#
|
7
7
|
# require 'mustache/sinatra'
|
8
8
|
#
|
9
|
-
# class
|
9
|
+
# class Hurl < Sinatra::Base
|
10
|
+
# register Mustache::Sinatra
|
11
|
+
#
|
10
12
|
# # Should be the path to your .mustache template files.
|
11
13
|
# set :views, "path/to/mustache/templates"
|
12
14
|
#
|
@@ -14,19 +16,21 @@ class Mustache
|
|
14
16
|
# # Only needed if different from the `views` setting
|
15
17
|
# set :mustaches, "path/to/mustache/views"
|
16
18
|
#
|
17
|
-
# # This tells Mustache where to look for the Views
|
19
|
+
# # This tells Mustache where to look for the Views module,
|
18
20
|
# # under which your View classes should live. By default it's
|
19
|
-
# #
|
20
|
-
# #
|
21
|
-
#
|
22
|
-
#
|
21
|
+
# # the class of your app - in this case `Hurl`. That is, for an :index
|
22
|
+
# # view Mustache will expect Hurl::Views::Index by default.
|
23
|
+
#
|
24
|
+
# # If our Sinatra::Base subclass was instead Hurl::App,
|
25
|
+
# # we'd want to do `set :namespace, Hurl::App`
|
26
|
+
# set :namespace, Hurl
|
23
27
|
#
|
24
28
|
# get '/stats' do
|
25
29
|
# mustache :stats
|
26
30
|
# end
|
27
31
|
# end
|
28
32
|
#
|
29
|
-
# As noted above, Mustache will look for `
|
33
|
+
# As noted above, Mustache will look for `Hurl::Views::Index` when
|
30
34
|
# `mustache :index` is called.
|
31
35
|
#
|
32
36
|
# If no `Views::Stats` class exists Mustache will render the template
|
@@ -36,61 +40,67 @@ class Mustache
|
|
36
40
|
# <%= yield %> you instead {{{yield}}} - the body of the subview is
|
37
41
|
# set to the `yield` variable and made available to you.
|
38
42
|
module Sinatra
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
+
module Helpers
|
44
|
+
# Call this in your Sinatra routes.
|
45
|
+
def mustache(template, options={}, locals={})
|
46
|
+
render :mustache, template, options, locals
|
47
|
+
end
|
43
48
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
49
|
+
# This is called by Sinatra's `render` with the proper paths
|
50
|
+
# and, potentially, a block containing a sub-view
|
51
|
+
def render_mustache(template, data, opts, locals, &block)
|
52
|
+
name = Mustache.classify(template.to_s)
|
48
53
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
54
|
+
# This is a horrible hack but we need it to know under which namespace
|
55
|
+
# Views is located. If you have Hurl::App::Views, namespace should be
|
56
|
+
# set to Hurl:App.
|
57
|
+
namespace = options.namespace
|
53
58
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
59
|
+
if namespace.const_defined?(:Views) && namespace::Views.const_defined?(name)
|
60
|
+
# First try to find the existing view,
|
61
|
+
# e.g. Hurl::Views::Index
|
62
|
+
klass = namespace::Views.const_get(name)
|
58
63
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
+
elsif File.exists?(file = "#{options.mustaches}/#{template}.rb")
|
65
|
+
# Couldn't find it - try to require the file if it exists, then
|
66
|
+
# load in the view.
|
67
|
+
require "#{file}".chomp('.rb')
|
68
|
+
klass = namespace::Views.const_get(name)
|
64
69
|
|
65
|
-
|
66
|
-
|
67
|
-
|
70
|
+
else
|
71
|
+
# Still nothing. Use the stache.
|
72
|
+
klass = Mustache
|
68
73
|
|
69
|
-
|
74
|
+
end
|
70
75
|
|
71
|
-
|
72
|
-
|
76
|
+
# Create a new instance for playing with
|
77
|
+
instance = klass.new
|
73
78
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
79
|
+
# Copy instance variables set in Sinatra to the view
|
80
|
+
instance_variables.each do |name|
|
81
|
+
instance.instance_variable_set(name, instance_variable_get(name))
|
82
|
+
end
|
78
83
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
84
|
+
# Locals get added to the view's context
|
85
|
+
locals.each do |local, value|
|
86
|
+
instance[local] = value
|
87
|
+
end
|
88
|
+
|
89
|
+
# If we're paseed a block it's a subview. Sticking it in yield
|
90
|
+
# lets us use {{yield}} in layout.html to render the actual page.
|
91
|
+
instance[:yield] = block.call if block
|
83
92
|
|
84
|
-
|
85
|
-
|
86
|
-
|
93
|
+
instance.template = data
|
94
|
+
instance.to_html
|
95
|
+
end
|
96
|
+
end
|
87
97
|
|
88
|
-
|
89
|
-
|
98
|
+
def self.registered(app)
|
99
|
+
app.helpers Mustache::Sinatra::Helpers
|
100
|
+
app.set :mustaches, ::Sinatra::Base.views
|
101
|
+
app.set :namespace, app
|
90
102
|
end
|
91
103
|
end
|
92
104
|
end
|
93
105
|
|
94
|
-
Sinatra
|
95
|
-
Sinatra::Base.set :mustaches, Sinatra::Base.views
|
96
|
-
Sinatra::Base.set :namespace, Object
|
106
|
+
Sinatra.register Mustache::Sinatra
|
data/lib/mustache/version.rb
CHANGED