mustache-sinatra 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mustache/sinatra.rb +3 -143
- data/lib/mustache/sinatra/helpers.rb +149 -0
- data/lib/mustache/sinatra/version.rb +1 -1
- data/mustache-sinatra.gemspec +2 -0
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0a10facd8c6cb6543e7a19ba8c8b5881b5af1907
|
4
|
+
data.tar.gz: c53a8137cd6dc6512b40aac096cb87e94bece9eb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4fff4466c64df75740c7ffd5c45e9fc6cf76386f5f0724801292d8426e75be987bf0a931ec3b5e36b408c5acc6b1bc717a523a56bd50a4be679f8c2604535b00
|
7
|
+
data.tar.gz: 05be9a0162b4f93588bdd79a52220decc4c057ce36aad1819f1260800dfdce3a367506c2455a234f8c3ff7351714c2a134aadfd1a4f390b799ad745b9489fa69
|
data/lib/mustache/sinatra.rb
CHANGED
@@ -1,149 +1,9 @@
|
|
1
|
-
require
|
1
|
+
require 'mustache/sinatra/version'
|
2
|
+
require 'mustache'
|
3
|
+
require 'mustache/sinatra/helpers'
|
2
4
|
|
3
5
|
class Mustache
|
4
6
|
module Sinatra
|
5
|
-
module Helpers
|
6
|
-
# Call this in your Sinatra routes.
|
7
|
-
def mustache(template, options={}, locals={})
|
8
|
-
# Locals can be passed as options under the :locals key.
|
9
|
-
locals.update(options.delete(:locals) || {})
|
10
|
-
|
11
|
-
# Grab any user-defined settings.
|
12
|
-
if settings.respond_to?(:mustache)
|
13
|
-
options = settings.send(:mustache).merge(options)
|
14
|
-
end
|
15
|
-
|
16
|
-
# If they aren't explicitly disabling layouts, try to find
|
17
|
-
# one.
|
18
|
-
if options[:layout] != false
|
19
|
-
# Let the user pass in a layout name.
|
20
|
-
layout_name = options[:layout]
|
21
|
-
|
22
|
-
# If all they said was `true` (or nothing), default to :layout.
|
23
|
-
layout_name = :layout if layout_name == true || !layout_name
|
24
|
-
|
25
|
-
# If they passed a layout name use that.
|
26
|
-
layout = mustache_class(layout_name, options)
|
27
|
-
|
28
|
-
# If it's just an anonymous subclass then don't bother, otherwise
|
29
|
-
# give us a layout instance.
|
30
|
-
if layout.name && layout.name.empty?
|
31
|
-
layout = nil
|
32
|
-
else
|
33
|
-
layout = layout.new
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
# If instead of a symbol they gave us a Mustache class,
|
38
|
-
# use that for rendering.
|
39
|
-
klass = template if template.is_a?(Class) && template < Mustache
|
40
|
-
|
41
|
-
# Find and cache the view class we want if we don't have
|
42
|
-
# one yet. This ensures the compiled template is cached,
|
43
|
-
# too - no looking up and compiling templates on each page
|
44
|
-
# load.
|
45
|
-
if klass.nil?
|
46
|
-
klass = mustache_class(template, options)
|
47
|
-
end
|
48
|
-
|
49
|
-
# Does the view subclass the layout? If so we'll use the
|
50
|
-
# view to render the layout so you can override layout
|
51
|
-
# methods in your view - tricky.
|
52
|
-
view_subclasses_layout = klass < layout.class if layout
|
53
|
-
|
54
|
-
# Create a new instance for playing with.
|
55
|
-
instance = klass.new
|
56
|
-
|
57
|
-
# Copy instance variables set in Sinatra to the view
|
58
|
-
instance_variables.each do |name|
|
59
|
-
instance.instance_variable_set(name, instance_variable_get(name))
|
60
|
-
end
|
61
|
-
|
62
|
-
# Render with locals.
|
63
|
-
rendered = instance.render(instance.template, locals)
|
64
|
-
|
65
|
-
# Now render the layout with the view we just rendered, if we
|
66
|
-
# need to.
|
67
|
-
if layout && view_subclasses_layout
|
68
|
-
rendered = instance.render(layout.template, :yield => rendered)
|
69
|
-
elsif layout
|
70
|
-
rendered = layout.render(layout.template, :yield => rendered)
|
71
|
-
end
|
72
|
-
|
73
|
-
# That's it.
|
74
|
-
rendered
|
75
|
-
end
|
76
|
-
|
77
|
-
# Returns a View class for a given template name.
|
78
|
-
def mustache_class(template, options = {})
|
79
|
-
@template_cache.fetch(:mustache, template) do
|
80
|
-
compile_mustache(template, options)
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
# Given a view name and settings, finds and prepares an
|
85
|
-
# appropriate view class for this view.
|
86
|
-
def compile_mustache(view, options = {})
|
87
|
-
options[:templates] ||= settings.views if settings.respond_to?(:views)
|
88
|
-
options[:namespace] ||= self.class
|
89
|
-
|
90
|
-
unless options[:namespace].to_s.include? 'Views'
|
91
|
-
options[:namespace] = options[:namespace].const_get(:Views) rescue Object
|
92
|
-
end
|
93
|
-
|
94
|
-
factory = Class.new(Mustache) do
|
95
|
-
self.view_namespace = options[:namespace]
|
96
|
-
self.view_path = options[:views]
|
97
|
-
end
|
98
|
-
|
99
|
-
# If we were handed :"positions.atom" or some such as the
|
100
|
-
# template name, we need to remember the extension.
|
101
|
-
if view.to_s.include?('.')
|
102
|
-
view, ext = view.to_s.split('.')
|
103
|
-
end
|
104
|
-
|
105
|
-
# Try to find the view class for a given view, e.g.
|
106
|
-
# :view => Hurl::Views::Index.
|
107
|
-
klass = factory.view_class(view)
|
108
|
-
klass.view_namespace = options[:namespace]
|
109
|
-
klass.view_path = options[:views]
|
110
|
-
|
111
|
-
# If there is no view class, issue a warning and use the one
|
112
|
-
# we just generated to cache the compiled template.
|
113
|
-
if klass == Mustache
|
114
|
-
warn "No view class found for #{view} in #{factory.view_path}"
|
115
|
-
klass = factory
|
116
|
-
|
117
|
-
# If this is a generic view class make sure we set the
|
118
|
-
# template name as it was given. That is, an anonymous
|
119
|
-
# subclass of Mustache won't know how to find the
|
120
|
-
# "index.mustache" template unless we tell it to.
|
121
|
-
klass.template_name = view.to_s
|
122
|
-
elsif ext
|
123
|
-
# We got an ext (like "atom"), so look for an "Atom" class
|
124
|
-
# under the current View's namespace.
|
125
|
-
#
|
126
|
-
# So if our template was "positions.atom", try to find
|
127
|
-
# Positions::Atom.
|
128
|
-
if klass.const_defined?(ext_class = ext.capitalize)
|
129
|
-
# Found Positions::Atom - set it
|
130
|
-
klass = klass.const_get(ext_class)
|
131
|
-
else
|
132
|
-
# Didn't find Positions::Atom - create it by creating an
|
133
|
-
# anonymous subclass of Positions and setting that to
|
134
|
-
# Positions::Atom.
|
135
|
-
new_class = Class.new(klass)
|
136
|
-
new_class.template_name = "#{view}.#{ext}"
|
137
|
-
klass.const_set(ext_class, new_class)
|
138
|
-
klass = new_class
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
# Set the template path and return our class.
|
143
|
-
klass.template_path = options[:templates] if options[:templates]
|
144
|
-
klass
|
145
|
-
end
|
146
|
-
end
|
147
7
|
|
148
8
|
# Called when you `register Mustache::Sinatra` in your Sinatra app.
|
149
9
|
def self.registered(app)
|
@@ -0,0 +1,149 @@
|
|
1
|
+
class Mustache
|
2
|
+
module Sinatra
|
3
|
+
|
4
|
+
module Helpers
|
5
|
+
|
6
|
+
# Call this in your Sinatra routes.
|
7
|
+
def mustache(template, options={}, locals={})
|
8
|
+
# Locals can be passed as options under the :locals key.
|
9
|
+
locals.update(options.delete(:locals) || {})
|
10
|
+
|
11
|
+
# Grab any user-defined settings.
|
12
|
+
if settings.respond_to?(:mustache)
|
13
|
+
options = settings.send(:mustache).merge(options)
|
14
|
+
end
|
15
|
+
|
16
|
+
# If they aren't explicitly disabling layouts, try to find
|
17
|
+
# one.
|
18
|
+
if options[:layout] != false
|
19
|
+
# Let the user pass in a layout name.
|
20
|
+
layout_name = options[:layout]
|
21
|
+
|
22
|
+
# If all they said was `true` (or nothing), default to :layout.
|
23
|
+
layout_name = :layout if layout_name == true || !layout_name
|
24
|
+
|
25
|
+
# If they passed a layout name use that.
|
26
|
+
layout = mustache_class(layout_name, options)
|
27
|
+
|
28
|
+
# If it's just an anonymous subclass then don't bother, otherwise
|
29
|
+
# give us a layout instance.
|
30
|
+
if layout.name && layout.name.empty?
|
31
|
+
layout = nil
|
32
|
+
else
|
33
|
+
layout = layout.new
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# If instead of a symbol they gave us a Mustache class,
|
38
|
+
# use that for rendering.
|
39
|
+
klass = template if template.is_a?(Class) && template < Mustache
|
40
|
+
|
41
|
+
# Find and cache the view class we want if we don't have
|
42
|
+
# one yet. This ensures the compiled template is cached,
|
43
|
+
# too - no looking up and compiling templates on each page
|
44
|
+
# load.
|
45
|
+
if klass.nil?
|
46
|
+
klass = mustache_class(template, options)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Does the view subclass the layout? If so we'll use the
|
50
|
+
# view to render the layout so you can override layout
|
51
|
+
# methods in your view - tricky.
|
52
|
+
view_subclasses_layout = klass < layout.class if layout
|
53
|
+
|
54
|
+
# Create a new instance for playing with.
|
55
|
+
instance = klass.new
|
56
|
+
|
57
|
+
# Copy instance variables set in Sinatra to the view
|
58
|
+
instance_variables.each do |name|
|
59
|
+
instance.instance_variable_set(name, instance_variable_get(name))
|
60
|
+
end
|
61
|
+
|
62
|
+
# Render with locals.
|
63
|
+
rendered = instance.render(instance.template, locals)
|
64
|
+
|
65
|
+
# Now render the layout with the view we just rendered, if we
|
66
|
+
# need to.
|
67
|
+
if layout && view_subclasses_layout
|
68
|
+
rendered = instance.render(layout.template, :yield => rendered)
|
69
|
+
elsif layout
|
70
|
+
rendered = layout.render(layout.template, :yield => rendered)
|
71
|
+
end
|
72
|
+
|
73
|
+
# That's it.
|
74
|
+
rendered
|
75
|
+
end
|
76
|
+
|
77
|
+
# Returns a View class for a given template name.
|
78
|
+
def mustache_class(template, options = {})
|
79
|
+
@template_cache.fetch(:mustache, template) do
|
80
|
+
compile_mustache(template, options)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# Given a view name and settings, finds and prepares an
|
85
|
+
# appropriate view class for this view.
|
86
|
+
def compile_mustache(view, options = {})
|
87
|
+
options[:templates] ||= settings.views if settings.respond_to?(:views)
|
88
|
+
options[:namespace] ||= self.class
|
89
|
+
|
90
|
+
unless options[:namespace].to_s.include? 'Views'
|
91
|
+
options[:namespace] = options[:namespace].const_get(:Views) rescue Object
|
92
|
+
end
|
93
|
+
|
94
|
+
factory = Class.new(Mustache) do
|
95
|
+
self.view_namespace = options[:namespace]
|
96
|
+
self.view_path = options[:views]
|
97
|
+
end
|
98
|
+
|
99
|
+
# If we were handed :"positions.atom" or some such as the
|
100
|
+
# template name, we need to remember the extension.
|
101
|
+
if view.to_s.include?('.')
|
102
|
+
view, ext = view.to_s.split('.')
|
103
|
+
end
|
104
|
+
|
105
|
+
# Try to find the view class for a given view, e.g.
|
106
|
+
# :view => Hurl::Views::Index.
|
107
|
+
klass = factory.view_class(view)
|
108
|
+
klass.view_namespace = options[:namespace]
|
109
|
+
klass.view_path = options[:views]
|
110
|
+
|
111
|
+
# If there is no view class, issue a warning and use the one
|
112
|
+
# we just generated to cache the compiled template.
|
113
|
+
if klass == Mustache
|
114
|
+
warn "No view class found for #{view} in #{factory.view_path}"
|
115
|
+
klass = factory
|
116
|
+
|
117
|
+
# If this is a generic view class make sure we set the
|
118
|
+
# template name as it was given. That is, an anonymous
|
119
|
+
# subclass of Mustache won't know how to find the
|
120
|
+
# "index.mustache" template unless we tell it to.
|
121
|
+
klass.template_name = view.to_s
|
122
|
+
elsif ext
|
123
|
+
# We got an ext (like "atom"), so look for an "Atom" class
|
124
|
+
# under the current View's namespace.
|
125
|
+
#
|
126
|
+
# So if our template was "positions.atom", try to find
|
127
|
+
# Positions::Atom.
|
128
|
+
if klass.const_defined?(ext_class = ext.capitalize)
|
129
|
+
# Found Positions::Atom - set it
|
130
|
+
klass = klass.const_get(ext_class)
|
131
|
+
else
|
132
|
+
# Didn't find Positions::Atom - create it by creating an
|
133
|
+
# anonymous subclass of Positions and setting that to
|
134
|
+
# Positions::Atom.
|
135
|
+
new_class = Class.new(klass)
|
136
|
+
new_class.template_name = "#{view}.#{ext}"
|
137
|
+
klass.const_set(ext_class, new_class)
|
138
|
+
klass = new_class
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
# Set the template path and return our class.
|
143
|
+
klass.template_path = options[:templates] if options[:templates]
|
144
|
+
klass
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
end
|
149
|
+
end
|
data/mustache-sinatra.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mustache-sinatra
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ricardo Mendes
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: mustache
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "<="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.99.8
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "<="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.99.8
|
41
55
|
description: Mustache support for Sinatra applications
|
42
56
|
email:
|
43
57
|
- rokusu@gmail.com
|
@@ -52,6 +66,7 @@ files:
|
|
52
66
|
- README.md
|
53
67
|
- Rakefile
|
54
68
|
- lib/mustache/sinatra.rb
|
69
|
+
- lib/mustache/sinatra/helpers.rb
|
55
70
|
- lib/mustache/sinatra/version.rb
|
56
71
|
- mustache-sinatra.gemspec
|
57
72
|
homepage: https://github.com/mustache/mustache-sinatra
|