Skip to content

Commit 075fdb8

Browse files
committed
Merge pull request handlebars-lang#454 from leshill/fix_string_mode_contexts
Add contexts for string mode hash values
2 parents 4429ffa + 53de759 commit 075fdb8

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
@@ -1050,6 +1050,10 @@ Compiler.prototype = {
10501050
val = pair[1];
10511051

10521052
if (this.options.stringParams) {
1053+
if(val.depth) {
1054+
this.addDepth(val.depth);
1055+
}
1056+
this.opcode('getContext', val.depth || 0);
10531057
this.opcode('pushStringParam', val.stringModeValue, val.type);
10541058
} else {
10551059
this.accept(val);
@@ -1634,16 +1638,18 @@ JavaScriptCompiler.prototype = {
16341638

16351639
if (this.options.stringParams) {
16361640
this.register('hashTypes', '{}');
1641+
this.register('hashContexts', '{}');
16371642
}
16381643
},
16391644
pushHash: function() {
1640-
this.hash = {values: [], types: []};
1645+
this.hash = {values: [], types: [], contexts: []};
16411646
},
16421647
popHash: function() {
16431648
var hash = this.hash;
16441649
this.hash = undefined;
16451650

16461651
if (this.options.stringParams) {
1652+
this.register('hashContexts', '{' + hash.contexts.join(',') + '}');
16471653
this.register('hashTypes', '{' + hash.types.join(',') + '}');
16481654
}
16491655
this.push('{\n ' + hash.values.join(',\n ') + '\n }');
@@ -1786,14 +1792,18 @@ JavaScriptCompiler.prototype = {
17861792
// and pushes the hash back onto the stack.
17871793
assignToHash: function(key) {
17881794
var value = this.popStack(),
1795+
context,
17891796
type;
17901797

17911798
if (this.options.stringParams) {
17921799
type = this.popStack();
1793-
this.popStack();
1800+
context = this.popStack();
17941801
}
17951802

17961803
var hash = this.hash;
1804+
if (context) {
1805+
hash.contexts.push("'" + key + "': " + context);
1806+
}
17971807
if (type) {
17981808
hash.types.push("'" + key + "': " + type);
17991809
}
@@ -2044,6 +2054,7 @@ JavaScriptCompiler.prototype = {
20442054
if (this.options.stringParams) {
20452055
options.push("contexts:[" + contexts.join(",") + "]");
20462056
options.push("types:[" + types.join(",") + "]");
2057+
options.push("hashContexts:hashContexts");
20472058
options.push("hashTypes:hashTypes");
20482059
}
20492060

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
}
@@ -1183,6 +1193,7 @@ JavaScriptCompiler.prototype = {
11831193
if (this.options.stringParams) {
11841194
options.push("contexts:[" + contexts.join(",") + "]");
11851195
options.push("types:[" + types.join(",") + "]");
1196+
options.push("hashContexts:hashContexts");
11861197
options.push("hashTypes:hashTypes");
11871198
}
11881199

Diff for: spec/qunit_spec.js

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

1332+
test("in string mode, hash parameters get context information", function() {
1333+
var template = CompilerContext.compile('{{#with dale}}{{tomdale he.says desire="need" noun=../dad/joke bool=true}}{{/with}}', { stringParams: true });
1334+
1335+
var context = {dale: {}};
1336+
1337+
var helpers = {
1338+
tomdale: function(exclamation, options) {
1339+
equal(exclamation, "he.says");
1340+
equal(options.types[0], "ID");
1341+
1342+
equal(options.contexts.length, 1);
1343+
equal(options.hashContexts.noun, context);
1344+
equal(options.hash.desire, "need");
1345+
equal(options.hash.noun, "dad.joke");
1346+
equal(options.hash.bool, true);
1347+
return "Helper called";
1348+
},
1349+
"with": function(context, options) {
1350+
return options.fn(options.contexts[0][context]);
1351+
}
1352+
};
1353+
1354+
var result = template(context, { helpers: helpers });
1355+
equal(result, "Helper called");
1356+
});
1357+
13321358
test("when inside a block in String mode, .. passes the appropriate context in the options hash to a block helper", function() {
13331359
var template = CompilerContext.compile('{{#with dale}}{{#tomdale ../need dad.joke}}wot{{/tomdale}}{{/with}}', {stringParams: true});
13341360

0 commit comments

Comments
 (0)