Add support for getting types in string mode
This makes it possible to determine whether an argument was passed as a string or as a path when implementing helpers in string mode.
This commit is contained in:
@@ -129,7 +129,7 @@ Handlebars.JavaScriptCompiler = function() {};
|
||||
// evaluate it by executing `blockHelperMissing`
|
||||
this.opcode('pushProgram', program);
|
||||
this.opcode('pushProgram', inverse);
|
||||
this.opcode('pushLiteral', '{}');
|
||||
this.opcode('pushHash');
|
||||
this.opcode('blockValue');
|
||||
} else {
|
||||
this.ambiguousMustache(mustache, program, inverse);
|
||||
@@ -138,7 +138,7 @@ Handlebars.JavaScriptCompiler = function() {};
|
||||
// evaluate it by executing `blockHelperMissing`
|
||||
this.opcode('pushProgram', program);
|
||||
this.opcode('pushProgram', inverse);
|
||||
this.opcode('pushLiteral', '{}');
|
||||
this.opcode('pushHash');
|
||||
this.opcode('ambiguousBlockValue');
|
||||
}
|
||||
|
||||
@@ -148,13 +148,18 @@ Handlebars.JavaScriptCompiler = function() {};
|
||||
hash: function(hash) {
|
||||
var pairs = hash.pairs, pair, val;
|
||||
|
||||
this.opcode('push', '{}');
|
||||
this.opcode('pushHash');
|
||||
|
||||
for(var i=0, l=pairs.length; i<l; i++) {
|
||||
pair = pairs[i];
|
||||
val = pair[1];
|
||||
|
||||
this.accept(val);
|
||||
if (this.options.stringParams) {
|
||||
this.opcode('pushStringParam', val.stringModeValue, val.type);
|
||||
} else {
|
||||
this.accept(val);
|
||||
}
|
||||
|
||||
this.opcode('assignToHash', pair[0]);
|
||||
}
|
||||
},
|
||||
@@ -324,7 +329,7 @@ Handlebars.JavaScriptCompiler = function() {};
|
||||
}
|
||||
|
||||
this.opcode('getContext', param.depth || 0);
|
||||
this.opcode('pushStringParam', param.string);
|
||||
this.opcode('pushStringParam', param.stringModeValue, param.type);
|
||||
} else {
|
||||
this[param.type](param);
|
||||
}
|
||||
@@ -338,7 +343,7 @@ Handlebars.JavaScriptCompiler = function() {};
|
||||
if(mustache.hash) {
|
||||
this.hash(mustache.hash);
|
||||
} else {
|
||||
this.opcode('pushLiteral', '{}');
|
||||
this.opcode('pushHash');
|
||||
}
|
||||
|
||||
return params;
|
||||
@@ -355,7 +360,7 @@ Handlebars.JavaScriptCompiler = function() {};
|
||||
if(mustache.hash) {
|
||||
this.hash(mustache.hash);
|
||||
} else {
|
||||
this.opcode('pushLiteral', '{}');
|
||||
this.opcode('pushHash');
|
||||
}
|
||||
|
||||
return params;
|
||||
@@ -677,9 +682,24 @@ Handlebars.JavaScriptCompiler = function() {};
|
||||
// This opcode is designed for use in string mode, which
|
||||
// provides the string value of a parameter along with its
|
||||
// depth rather than resolving it immediately.
|
||||
pushStringParam: function(string) {
|
||||
pushStringParam: function(string, type) {
|
||||
this.pushStackLiteral('depth' + this.lastContext);
|
||||
this.pushString(string);
|
||||
|
||||
this.pushString(type);
|
||||
|
||||
if (typeof string === 'string') {
|
||||
this.pushString(string);
|
||||
} else {
|
||||
this.pushStackLiteral(string);
|
||||
}
|
||||
},
|
||||
|
||||
pushHash: function() {
|
||||
this.push('{}');
|
||||
|
||||
if (this.options.stringParams) {
|
||||
this.register('hashTypes', '{}');
|
||||
}
|
||||
},
|
||||
|
||||
// [pushString]
|
||||
@@ -817,6 +837,12 @@ Handlebars.JavaScriptCompiler = function() {};
|
||||
// and pushes the hash back onto the stack.
|
||||
assignToHash: function(key) {
|
||||
var value = this.popStack();
|
||||
|
||||
if (this.options.stringParams) {
|
||||
var type = this.popStack();
|
||||
this.source.push("hashTypes['" + key + "'] = " + type + ";");
|
||||
}
|
||||
|
||||
var hash = this.topStack();
|
||||
|
||||
this.source.push(hash + "['" + key + "'] = " + value + ";");
|
||||
@@ -962,7 +988,7 @@ Handlebars.JavaScriptCompiler = function() {};
|
||||
// the params and contexts arguments are passed in arrays
|
||||
// to fill in
|
||||
setupParams: function(paramSize, params) {
|
||||
var options = [], contexts = [], param, inverse, program;
|
||||
var options = [], contexts = [], types = [], param, inverse, program;
|
||||
|
||||
options.push("hash:" + this.popStack());
|
||||
|
||||
@@ -991,12 +1017,15 @@ Handlebars.JavaScriptCompiler = function() {};
|
||||
params.push(param);
|
||||
|
||||
if(this.options.stringParams) {
|
||||
types.push(this.popStack());
|
||||
contexts.push(this.popStack());
|
||||
}
|
||||
}
|
||||
|
||||
if (this.options.stringParams) {
|
||||
options.push("contexts:[" + contexts.join(",") + "]");
|
||||
options.push("types:[" + types.join(",") + "]");
|
||||
options.push("hashTypes:hashTypes");
|
||||
}
|
||||
|
||||
if(this.options.data) {
|
||||
|
||||
Reference in New Issue
Block a user