Skip to content

Commit 53de759

Browse files
committed
Add contexts for string mode hash values
Allows for evaluating hash parameters such as ../city in string mode.
1 parent bcc15ea commit 53de759

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

Diff for: dist/handlebars.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,10 @@ Compiler.prototype = {
10231023
val = pair[1];
10241024

10251025
if (this.options.stringParams) {
1026+
if(val.depth) {
1027+
this.addDepth(val.depth);
1028+
}
1029+
this.opcode('getContext', val.depth || 0);
10261030
this.opcode('pushStringParam', val.stringModeValue, val.type);
10271031
} else {
10281032
this.accept(val);
@@ -1607,16 +1611,18 @@ JavaScriptCompiler.prototype = {
16071611

16081612
if (this.options.stringParams) {
16091613
this.register('hashTypes', '{}');
1614+
this.register('hashContexts', '{}');
16101615
}
16111616
},
16121617
pushHash: function() {
1613-
this.hash = {values: [], types: []};
1618+
this.hash = {values: [], types: [], contexts: []};
16141619
},
16151620
popHash: function() {
16161621
var hash = this.hash;
16171622
this.hash = undefined;
16181623

16191624
if (this.options.stringParams) {
1625+
this.register('hashContexts', '{' + hash.contexts.join(',') + '}');
16201626
this.register('hashTypes', '{' + hash.types.join(',') + '}');
16211627
}
16221628
this.push('{\n ' + hash.values.join(',\n ') + '\n }');
@@ -1759,14 +1765,18 @@ JavaScriptCompiler.prototype = {
17591765
// and pushes the hash back onto the stack.
17601766
assignToHash: function(key) {
17611767
var value = this.popStack(),
1768+
context,
17621769
type;
17631770

17641771
if (this.options.stringParams) {
17651772
type = this.popStack();
1766-
this.popStack();
1773+
context = this.popStack();
17671774
}
17681775

17691776
var hash = this.hash;
1777+
if (context) {
1778+
hash.contexts.push("'" + key + "': " + context);
1779+
}
17701780
if (type) {
17711781
hash.types.push("'" + key + "': " + type);
17721782
}
@@ -2020,6 +2030,7 @@ JavaScriptCompiler.prototype = {
20202030
if (this.options.stringParams) {
20212031
options.push("contexts:[" + contexts.join(",") + "]");
20222032
options.push("types:[" + types.join(",") + "]");
2033+
options.push("hashContexts:hashContexts");
20232034
options.push("hashTypes:hashTypes");
20242035
}
20252036

Diff for: lib/handlebars/compiler/compiler.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ Compiler.prototype = {
189189
val = pair[1];
190190

191191
if (this.options.stringParams) {
192+
if(val.depth) {
193+
this.addDepth(val.depth);
194+
}
195+
this.opcode('getContext', val.depth || 0);
192196
this.opcode('pushStringParam', val.stringModeValue, val.type);
193197
} else {
194198
this.accept(val);
@@ -773,16 +777,18 @@ JavaScriptCompiler.prototype = {
773777

774778
if (this.options.stringParams) {
775779
this.register('hashTypes', '{}');
780+
this.register('hashContexts', '{}');
776781
}
777782
},
778783
pushHash: function() {
779-
this.hash = {values: [], types: []};
784+
this.hash = {values: [], types: [], contexts: []};
780785
},
781786
popHash: function() {
782787
var hash = this.hash;
783788
this.hash = undefined;
784789

785790
if (this.options.stringParams) {
791+
this.register('hashContexts', '{' + hash.contexts.join(',') + '}');
786792
this.register('hashTypes', '{' + hash.types.join(',') + '}');
787793
}
788794
this.push('{\n ' + hash.values.join(',\n ') + '\n }');
@@ -925,14 +931,18 @@ JavaScriptCompiler.prototype = {
925931
// and pushes the hash back onto the stack.
926932
assignToHash: function(key) {
927933
var value = this.popStack(),
934+
context,
928935
type;
929936

930937
if (this.options.stringParams) {
931938
type = this.popStack();
932-
this.popStack();
939+
context = this.popStack();
933940
}
934941

935942
var hash = this.hash;
943+
if (context) {
944+
hash.contexts.push("'" + key + "': " + context);
945+
}
936946
if (type) {
937947
hash.types.push("'" + key + "': " + type);
938948
}
@@ -1186,6 +1196,7 @@ JavaScriptCompiler.prototype = {
11861196
if (this.options.stringParams) {
11871197
options.push("contexts:[" + contexts.join(",") + "]");
11881198
options.push("types:[" + types.join(",") + "]");
1199+
options.push("hashContexts:hashContexts");
11891200
options.push("hashTypes:hashTypes");
11901201
}
11911202

Diff for: spec/qunit_spec.js

+26
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,32 @@ test("in string mode, hash parameters get type information", function() {
12861286
equal(result, "Helper called");
12871287
});
12881288

1289+
test("in string mode, hash parameters get context information", function() {
1290+
var template = CompilerContext.compile('{{#with dale}}{{tomdale he.says desire="need" noun=../dad/joke bool=true}}{{/with}}', { stringParams: true });
1291+
1292+
var context = {dale: {}};
1293+
1294+
var helpers = {
1295+
tomdale: function(exclamation, options) {
1296+
equal(exclamation, "he.says");
1297+
equal(options.types[0], "ID");
1298+
1299+
equal(options.contexts.length, 1);
1300+
equal(options.hashContexts.noun, context);
1301+
equal(options.hash.desire, "need");
1302+
equal(options.hash.noun, "dad.joke");
1303+
equal(options.hash.bool, true);
1304+
return "Helper called";
1305+
},
1306+
"with": function(context, options) {
1307+
return options.fn(options.contexts[0][context]);
1308+
}
1309+
};
1310+
1311+
var result = template(context, { helpers: helpers });
1312+
equal(result, "Helper called");
1313+
});
1314+
12891315
test("when inside a block in String mode, .. passes the appropriate context in the options hash to a block helper", function() {
12901316
var template = CompilerContext.compile('{{#with dale}}{{#tomdale ../need dad.joke}}wot{{/tomdale}}{{/with}}', {stringParams: true});
12911317

0 commit comments

Comments
 (0)