Add Hash arguments to simple mustaches (TODO: add Hash args to block helpers)
This commit is contained in:
+47
-6
@@ -19,7 +19,8 @@ Handlebars.JavaScriptCompiler = function() {};
|
||||
invokeProgram: 11,
|
||||
invokePartial: 12,
|
||||
push: 13,
|
||||
invokeInverse: 14
|
||||
invokeInverse: 14,
|
||||
assignToHash: 15
|
||||
};
|
||||
|
||||
Compiler.MULTI_PARAM_OPCODES = {
|
||||
@@ -34,7 +35,8 @@ Handlebars.JavaScriptCompiler = function() {};
|
||||
invokeProgram: 2,
|
||||
invokePartial: 1,
|
||||
push: 1,
|
||||
invokeInverse: 1
|
||||
invokeInverse: 1,
|
||||
assignToHash: 1
|
||||
};
|
||||
|
||||
Compiler.DISASSEMBLE_MAP = {};
|
||||
@@ -163,6 +165,20 @@ Handlebars.JavaScriptCompiler = function() {};
|
||||
this.opcode('append');
|
||||
},
|
||||
|
||||
hash: function(hash) {
|
||||
var pairs = hash.pairs, pair, val;
|
||||
|
||||
this.opcode('push', '{}');
|
||||
|
||||
for(var i=0, l=pairs.length; i<l; i++) {
|
||||
pair = pairs[i];
|
||||
val = pair[1];
|
||||
|
||||
this.accept(val);
|
||||
this.opcode('assignToHash', pair[0]);
|
||||
}
|
||||
},
|
||||
|
||||
partial: function(partial) {
|
||||
var id = partial.id;
|
||||
this.usePartial = true;
|
||||
@@ -185,6 +201,13 @@ Handlebars.JavaScriptCompiler = function() {};
|
||||
var params = mustache.params;
|
||||
|
||||
this.pushParams(params);
|
||||
|
||||
if(mustache.hash) {
|
||||
this.hash(mustache.hash);
|
||||
} else {
|
||||
this.opcode('push', '{}');
|
||||
}
|
||||
|
||||
this.ID(mustache.id);
|
||||
|
||||
this.opcode('invokeMustache', params.length, mustache.id.original);
|
||||
@@ -466,11 +489,19 @@ Handlebars.JavaScriptCompiler = function() {};
|
||||
|
||||
fn = this.popStack();
|
||||
|
||||
var hash = this.popStack();
|
||||
|
||||
for(var i=0; i<paramSize; i++) {
|
||||
params.push(this.popStack());
|
||||
}
|
||||
|
||||
if(this.data) { params.push("data"); }
|
||||
this.register('tmp1', '{hash: ' + hash + '}');
|
||||
|
||||
if(this.data) {
|
||||
this.source.push('tmp1.data = data');
|
||||
}
|
||||
|
||||
params.push('tmp1');
|
||||
|
||||
var paramString = params.join(", ");
|
||||
var helperMissing = ["context"].concat(this.quotedString(original)).concat(params.slice(1));
|
||||
@@ -479,7 +510,7 @@ Handlebars.JavaScriptCompiler = function() {};
|
||||
|
||||
if(paramSize === 0) {
|
||||
// TODO: This case is not listed in the mustache spec. Is it important?
|
||||
this.source.push("if(typeof " + fn + " === 'function') { " + slot + " = " + fn + ".call(" + paramString + "); }");
|
||||
this.source.push("if(typeof " + fn + " === 'function') { " + slot + " = " + fn + ".call(" + paramString + "); } else { " + slot + " = " + fn + "; }");
|
||||
} else {
|
||||
this.source.push("if(typeof " + fn + " === 'function') { " + slot + " = " + fn + ".call(" + paramString + "); } else { " + slot + " = helpers.helperMissing.call(" + helperMissing + ") }");
|
||||
}
|
||||
@@ -506,8 +537,11 @@ Handlebars.JavaScriptCompiler = function() {};
|
||||
blockMissingParams.push(mainProgram, inverse);
|
||||
|
||||
if(this.data) {
|
||||
params.push("data");
|
||||
blockMissingParams.push("data");
|
||||
this.register('tmp1', '{data: data}');
|
||||
params.push("tmp1");
|
||||
|
||||
params.push("tmp1");
|
||||
blockMissingParams.push("tmp1");
|
||||
}
|
||||
|
||||
var nextStack = this.nextStack();
|
||||
@@ -527,6 +561,13 @@ Handlebars.JavaScriptCompiler = function() {};
|
||||
this.pushStack("this.invokePartial(" + this.nameLookup('partials', context, 'partial') + ", '" + context + "', " + this.popStack() + ", helpers, partials);");
|
||||
},
|
||||
|
||||
assignToHash: function(key) {
|
||||
var value = this.popStack();
|
||||
var hash = this.topStack();
|
||||
|
||||
this.source.push(hash + "['" + key + "'] = " + value + ";");
|
||||
},
|
||||
|
||||
// HELPERS
|
||||
|
||||
compiler: JavaScriptCompiler,
|
||||
|
||||
Reference in New Issue
Block a user