handlebars_assets 0.6.1 → 0.6.2
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/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/lib/handlebars_assets.rb
CHANGED
@@ -2,24 +2,40 @@ module HandlebarsAssets
|
|
2
2
|
# Change config options in an initializer:
|
3
3
|
#
|
4
4
|
# HandlebarsAssets::Config.path_prefix = 'app/templates'
|
5
|
-
#
|
6
|
-
# Or in a block:
|
7
|
-
#
|
8
|
-
# HandlebarsAssets::Config.configure do |config|
|
9
|
-
# path_prefix = 'app/templates'
|
10
|
-
# end
|
11
|
-
|
12
5
|
module Config
|
13
6
|
extend self
|
14
7
|
|
15
|
-
|
16
|
-
|
8
|
+
attr_writer :known_helpers, :known_helpers_only, :path_prefix
|
9
|
+
|
10
|
+
def known_helpers
|
11
|
+
@known_helpers || []
|
12
|
+
end
|
13
|
+
|
14
|
+
def known_helpers_only
|
15
|
+
@known_helpers_only || false
|
17
16
|
end
|
18
17
|
|
19
|
-
|
18
|
+
def options
|
19
|
+
options = {}
|
20
|
+
options[:knownHelpersOnly] = true if known_helpers_only
|
21
|
+
options[:knownHelpers] = known_helpers_hash if known_helpers_hash.any?
|
22
|
+
options
|
23
|
+
end
|
20
24
|
|
21
25
|
def path_prefix
|
22
26
|
@path_prefix ||= 'templates'
|
23
27
|
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def generate_known_helpers_hash
|
32
|
+
known_helpers.inject({}) do |hash, helper|
|
33
|
+
hash[helper] = true
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def known_helpers_hash
|
38
|
+
@known_helpers_hash ||= generate_known_helpers_hash
|
39
|
+
end
|
24
40
|
end
|
25
41
|
end
|
@@ -1,4 +1,3 @@
|
|
1
|
-
require 'handlebars_assets/config'
|
2
1
|
require 'tilt'
|
3
2
|
|
4
3
|
module HandlebarsAssets
|
@@ -10,7 +9,7 @@ module HandlebarsAssets
|
|
10
9
|
def evaluate(scope, locals, &block)
|
11
10
|
template_path = TemplatePath.new(scope)
|
12
11
|
|
13
|
-
compiled_hbs = Handlebars.precompile(data)
|
12
|
+
compiled_hbs = Handlebars.precompile(data, HandlebarsAssets::Config.options)
|
14
13
|
|
15
14
|
if template_path.is_partial?
|
16
15
|
<<-PARTIAL
|
@@ -54,6 +54,165 @@ END_EXPECTED
|
|
54
54
|
END_EXPECTED
|
55
55
|
end
|
56
56
|
|
57
|
+
def hbs_compiled_without_helper_opt(template_name, helper_name)
|
58
|
+
<<END_EXPECTED
|
59
|
+
(function() {
|
60
|
+
this.HandlebarsTemplates || (this.HandlebarsTemplates = {});
|
61
|
+
this.HandlebarsTemplates[\"#{template_name}\"] = Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
|
62
|
+
helpers = helpers || Handlebars.helpers;
|
63
|
+
var stack1, stack2, foundHelper, tmp1, self=this, functionType=\"function\", helperMissing=helpers.helperMissing, undef=void 0, escapeExpression=this.escapeExpression;
|
64
|
+
|
65
|
+
function program1(depth0,data) {
|
66
|
+
|
67
|
+
var buffer = \"\", stack1;
|
68
|
+
buffer += \"By \";
|
69
|
+
foundHelper = helpers.first_name;
|
70
|
+
stack1 = foundHelper || depth0.first_name;
|
71
|
+
if(typeof stack1 === functionType) { stack1 = stack1.call(depth0, { hash: {} }); }
|
72
|
+
else if(stack1=== undef) { stack1 = helperMissing.call(depth0, \"first_name\", { hash: {} }); }
|
73
|
+
buffer += escapeExpression(stack1) + \" \";
|
74
|
+
foundHelper = helpers.last_name;
|
75
|
+
stack1 = foundHelper || depth0.last_name;
|
76
|
+
if(typeof stack1 === functionType) { stack1 = stack1.call(depth0, { hash: {} }); }
|
77
|
+
else if(stack1=== undef) { stack1 = helperMissing.call(depth0, \"last_name\", { hash: {} }); }
|
78
|
+
buffer += escapeExpression(stack1);
|
79
|
+
return buffer;}
|
80
|
+
|
81
|
+
foundHelper = helpers.author;
|
82
|
+
stack1 = foundHelper || depth0.author;
|
83
|
+
stack2 = helpers['#{helper_name}'];
|
84
|
+
tmp1 = self.program(1, program1, data);
|
85
|
+
tmp1.hash = {};
|
86
|
+
tmp1.fn = tmp1;
|
87
|
+
tmp1.inverse = self.noop;
|
88
|
+
stack1 = stack2.call(depth0, stack1, tmp1);
|
89
|
+
if(stack1 || stack1 === 0) { return stack1; }
|
90
|
+
else { return ''; }});
|
91
|
+
return HandlebarsTemplates[\"#{template_name}\"];
|
92
|
+
}).call(this);
|
93
|
+
END_EXPECTED
|
94
|
+
end
|
95
|
+
|
96
|
+
def hbs_compiled_with_helper_opt(template_name, helper_name)
|
97
|
+
<<END_EXPECTED
|
98
|
+
(function() {
|
99
|
+
this.HandlebarsTemplates || (this.HandlebarsTemplates = {});
|
100
|
+
this.HandlebarsTemplates[\"#{template_name}\"] = Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
|
101
|
+
helpers = helpers || Handlebars.helpers;
|
102
|
+
var stack1, stack2, foundHelper, tmp1, self=this, functionType=\"function\", helperMissing=helpers.helperMissing, undef=void 0, escapeExpression=this.escapeExpression;
|
103
|
+
|
104
|
+
function program1(depth0,data) {
|
105
|
+
|
106
|
+
var buffer = \"\", stack1;
|
107
|
+
buffer += \"By \";
|
108
|
+
stack1 = depth0.first_name;
|
109
|
+
if(typeof stack1 === functionType) { stack1 = stack1.call(depth0, { hash: {} }); }
|
110
|
+
else if(stack1=== undef) { stack1 = helperMissing.call(depth0, \"first_name\", { hash: {} }); }
|
111
|
+
buffer += escapeExpression(stack1) + \" \";
|
112
|
+
stack1 = depth0.last_name;
|
113
|
+
if(typeof stack1 === functionType) { stack1 = stack1.call(depth0, { hash: {} }); }
|
114
|
+
else if(stack1=== undef) { stack1 = helperMissing.call(depth0, \"last_name\", { hash: {} }); }
|
115
|
+
buffer += escapeExpression(stack1);
|
116
|
+
return buffer;}
|
117
|
+
|
118
|
+
stack1 = depth0.author;
|
119
|
+
stack2 = helpers['#{helper_name}'];
|
120
|
+
tmp1 = self.program(1, program1, data);
|
121
|
+
tmp1.hash = {};
|
122
|
+
tmp1.fn = tmp1;
|
123
|
+
tmp1.inverse = self.noop;
|
124
|
+
stack1 = stack2.call(depth0, stack1, tmp1);
|
125
|
+
if(stack1 || stack1 === 0) { return stack1; }
|
126
|
+
else { return ''; }});
|
127
|
+
return HandlebarsTemplates[\"#{template_name}\"];
|
128
|
+
}).call(this);
|
129
|
+
END_EXPECTED
|
130
|
+
end
|
131
|
+
|
132
|
+
def hbs_custom_compiled_without_helper_opt(template_name, helper_name)
|
133
|
+
<<END_EXPECTED
|
134
|
+
(function() {
|
135
|
+
this.HandlebarsTemplates || (this.HandlebarsTemplates = {});
|
136
|
+
this.HandlebarsTemplates[\"#{template_name}\"] = Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
|
137
|
+
helpers = helpers || Handlebars.helpers;
|
138
|
+
var stack1, stack2, foundHelper, tmp1, self=this, functionType=\"function\", helperMissing=helpers.helperMissing, undef=void 0, escapeExpression=this.escapeExpression, blockHelperMissing=helpers.blockHelperMissing;
|
139
|
+
|
140
|
+
function program1(depth0,data) {
|
141
|
+
|
142
|
+
var buffer = \"\", stack1;
|
143
|
+
buffer += \"By \";
|
144
|
+
foundHelper = helpers.first_name;
|
145
|
+
stack1 = foundHelper || depth0.first_name;
|
146
|
+
if(typeof stack1 === functionType) { stack1 = stack1.call(depth0, { hash: {} }); }
|
147
|
+
else if(stack1=== undef) { stack1 = helperMissing.call(depth0, \"first_name\", { hash: {} }); }
|
148
|
+
buffer += escapeExpression(stack1) + \" \";
|
149
|
+
foundHelper = helpers.last_name;
|
150
|
+
stack1 = foundHelper || depth0.last_name;
|
151
|
+
if(typeof stack1 === functionType) { stack1 = stack1.call(depth0, { hash: {} }); }
|
152
|
+
else if(stack1=== undef) { stack1 = helperMissing.call(depth0, \"last_name\", { hash: {} }); }
|
153
|
+
buffer += escapeExpression(stack1);
|
154
|
+
return buffer;}
|
155
|
+
|
156
|
+
foundHelper = helpers.author;
|
157
|
+
stack1 = foundHelper || depth0.author;
|
158
|
+
foundHelper = helpers.#{helper_name};
|
159
|
+
stack2 = foundHelper || depth0.#{helper_name};
|
160
|
+
tmp1 = self.program(1, program1, data);
|
161
|
+
tmp1.hash = {};
|
162
|
+
tmp1.fn = tmp1;
|
163
|
+
tmp1.inverse = self.noop;
|
164
|
+
if(foundHelper && typeof stack2 === functionType) { stack1 = stack2.call(depth0, stack1, tmp1); }
|
165
|
+
else { stack1 = blockHelperMissing.call(depth0, stack2, stack1, tmp1); }
|
166
|
+
if(stack1 || stack1 === 0) { return stack1; }
|
167
|
+
else { return ''; }});
|
168
|
+
return HandlebarsTemplates[\"#{template_name}\"];
|
169
|
+
}).call(this);
|
170
|
+
END_EXPECTED
|
171
|
+
end
|
172
|
+
|
173
|
+
def hbs_custom_compiled_with_helper_opt(template_name, helper_name)
|
174
|
+
<<END_EXPECTED
|
175
|
+
(function() {
|
176
|
+
this.HandlebarsTemplates || (this.HandlebarsTemplates = {});
|
177
|
+
this.HandlebarsTemplates[\"#{template_name}\"] = Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
|
178
|
+
helpers = helpers || Handlebars.helpers;
|
179
|
+
var stack1, stack2, foundHelper, tmp1, self=this, functionType=\"function\", helperMissing=helpers.helperMissing, undef=void 0, escapeExpression=this.escapeExpression, blockHelperMissing=helpers.blockHelperMissing;
|
180
|
+
|
181
|
+
function program1(depth0,data) {
|
182
|
+
|
183
|
+
var buffer = \"\", stack1;
|
184
|
+
buffer += \"By \";
|
185
|
+
stack1 = depth0.first_name;
|
186
|
+
if(typeof stack1 === functionType) { stack1 = stack1.call(depth0, { hash: {} }); }
|
187
|
+
else if(stack1=== undef) { stack1 = helperMissing.call(depth0, \"first_name\", { hash: {} }); }
|
188
|
+
buffer += escapeExpression(stack1) + \" \";
|
189
|
+
stack1 = depth0.last_name;
|
190
|
+
if(typeof stack1 === functionType) { stack1 = stack1.call(depth0, { hash: {} }); }
|
191
|
+
else if(stack1=== undef) { stack1 = helperMissing.call(depth0, \"last_name\", { hash: {} }); }
|
192
|
+
buffer += escapeExpression(stack1);
|
193
|
+
return buffer;}
|
194
|
+
|
195
|
+
stack1 = depth0.author;
|
196
|
+
stack2 = depth0.#{helper_name};
|
197
|
+
tmp1 = self.program(1, program1, data);
|
198
|
+
tmp1.hash = {};
|
199
|
+
tmp1.fn = tmp1;
|
200
|
+
tmp1.inverse = self.noop;
|
201
|
+
if(foundHelper && typeof stack2 === functionType) { stack1 = stack2.call(depth0, stack1, tmp1); }
|
202
|
+
else { stack1 = blockHelperMissing.call(depth0, stack2, stack1, tmp1); }
|
203
|
+
if(stack1 || stack1 === 0) { return stack1; }
|
204
|
+
else { return ''; }});
|
205
|
+
return HandlebarsTemplates[\"#{template_name}\"];
|
206
|
+
}).call(this);
|
207
|
+
END_EXPECTED
|
208
|
+
end
|
209
|
+
|
210
|
+
def teardown
|
211
|
+
HandlebarsAssets::Config.known_helpers = []
|
212
|
+
HandlebarsAssets::Config.known_helpers_only = false
|
213
|
+
HandlebarsAssets::Config.path_prefix = nil
|
214
|
+
end
|
215
|
+
|
57
216
|
def test_render
|
58
217
|
root = '/myapp/app/assets/templates'
|
59
218
|
file = 'test_render.hbs'
|
@@ -86,8 +245,6 @@ END_EXPECTED
|
|
86
245
|
template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { "This is {{handlebars}}" }
|
87
246
|
|
88
247
|
assert_equal hbs_compiled('test_path_prefix'), template.render(scope, {})
|
89
|
-
|
90
|
-
HandlebarsAssets::Config.path_prefix = nil
|
91
248
|
end
|
92
249
|
|
93
250
|
def test_underscore_partials
|
@@ -106,8 +263,51 @@ END_EXPECTED
|
|
106
263
|
template2 = HandlebarsAssets::TiltHandlebars.new(scope2.pathname.to_s) { "This is {{handlebars}}" }
|
107
264
|
|
108
265
|
assert_equal hbs_compiled_partial('_some_thing_test_underscore'), template2.render(scope2, {})
|
266
|
+
end
|
109
267
|
|
110
|
-
|
268
|
+
def test_without_known_helpers_opt
|
269
|
+
root = '/myapp/app/assets/templates'
|
270
|
+
file = 'test_without_known.hbs'
|
271
|
+
scope = make_scope root, file
|
272
|
+
|
273
|
+
template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { "{{#with author}}By {{first_name}} {{last_name}}{{/with}}" }
|
274
|
+
|
275
|
+
assert_equal hbs_compiled_without_helper_opt('test_without_known', 'with'), template.render(scope, {})
|
276
|
+
end
|
277
|
+
|
278
|
+
def test_known_helpers_opt
|
279
|
+
root = '/myapp/app/assets/templates'
|
280
|
+
file = 'test_known.hbs'
|
281
|
+
scope = make_scope root, file
|
282
|
+
|
283
|
+
HandlebarsAssets::Config.known_helpers_only = true
|
284
|
+
|
285
|
+
template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { "{{#with author}}By {{first_name}} {{last_name}}{{/with}}" }
|
286
|
+
|
287
|
+
assert_equal hbs_compiled_with_helper_opt('test_known', 'with'), template.render(scope, {})
|
288
|
+
end
|
289
|
+
|
290
|
+
def test_with_custom_helpers
|
291
|
+
root = '/myapp/app/assets/templates'
|
292
|
+
file = 'test_custom_helper.hbs'
|
293
|
+
scope = make_scope root, file
|
294
|
+
|
295
|
+
template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { "{{#custom author}}By {{first_name}} {{last_name}}{{/custom}}" }
|
296
|
+
|
297
|
+
assert_equal hbs_custom_compiled_without_helper_opt('test_custom_helper', 'custom'), template.render(scope, {})
|
298
|
+
end
|
299
|
+
|
300
|
+
def test_with_custom_known_helpers
|
301
|
+
root = '/myapp/app/assets/templates'
|
302
|
+
file = 'test_custom_known_helper.hbs'
|
303
|
+
scope = make_scope root, file
|
304
|
+
|
305
|
+
HandlebarsAssets::Config.known_helpers_only = true
|
306
|
+
HandlebarsAssets::Config.known_helpers = %w(custom)
|
307
|
+
|
308
|
+
template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { "{{#custom author}}By {{first_name}} {{last_name}}{{/custom}}" }
|
309
|
+
|
310
|
+
assert_equal hbs_custom_compiled_with_helper_opt('test_custom_known_helper', 'custom'), template.render(scope, {})
|
111
311
|
end
|
112
312
|
end
|
113
313
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: handlebars_assets
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
12
|
+
date: 2012-07-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: execjs
|