summaryrefslogtreecommitdiffstats
path: root/lib/handlebars/compiler/javascript-compiler.js
diff options
context:
space:
mode:
authorkpdecker <kpdecker@gmail.com>2014-11-27 09:11:03 -0600
committerkpdecker <kpdecker@gmail.com>2014-11-27 09:11:03 -0600
commitf990cf006422fbcbbae7d8a8a866831e58300ea4 (patch)
tree5511ccfde2f01f1018f6a73576ef393590cf5d5a /lib/handlebars/compiler/javascript-compiler.js
parent1124908d2a0d36493912eb2ce062545b1b5e5445 (diff)
downloadhandlebars.js-f990cf006422fbcbbae7d8a8a866831e58300ea4.zip
handlebars.js-f990cf006422fbcbbae7d8a8a866831e58300ea4.tar.gz
handlebars.js-f990cf006422fbcbbae7d8a8a866831e58300ea4.tar.bz2
Treat partial exec in a manner closer to helpers
This helps unify the code handling and will also be needed to support string/id tracking on partials.
Diffstat (limited to 'lib/handlebars/compiler/javascript-compiler.js')
-rw-r--r--lib/handlebars/compiler/javascript-compiler.js41
1 files changed, 25 insertions, 16 deletions
diff --git a/lib/handlebars/compiler/javascript-compiler.js b/lib/handlebars/compiler/javascript-compiler.js
index cde2ab5..db19778 100644
--- a/lib/handlebars/compiler/javascript-compiler.js
+++ b/lib/handlebars/compiler/javascript-compiler.js
@@ -275,7 +275,7 @@ JavaScriptCompiler.prototype = {
blockValue: function(name) {
var blockHelperMissing = this.aliasable('helpers.blockHelperMissing'),
params = [this.contextName(0)];
- this.setupParams(name, 0, params);
+ this.setupHelperArgs(name, 0, params);
var blockName = this.popStack();
params.splice(1, 0, blockName);
@@ -293,7 +293,7 @@ JavaScriptCompiler.prototype = {
// We're being a bit cheeky and reusing the options value from the prior exec
var blockHelperMissing = this.aliasable('helpers.blockHelperMissing'),
params = [this.contextName(0)];
- this.setupParams('', 0, params, true);
+ this.setupHelperArgs('', 0, params, true);
this.flushInline();
@@ -469,9 +469,7 @@ JavaScriptCompiler.prototype = {
}
},
- emptyHash: function() {
- this.pushStackLiteral('{}');
-
+ emptyHash: function(omitEmpty) {
if (this.trackIds) {
this.push('{}'); // hashIds
}
@@ -479,6 +477,7 @@ JavaScriptCompiler.prototype = {
this.push('{}'); // hashContexts
this.push('{}'); // hashTypes
}
+ this.pushStackLiteral(omitEmpty ? 'undefined' : '{}');
},
pushHash: function() {
if (this.hash) {
@@ -611,16 +610,22 @@ JavaScriptCompiler.prototype = {
// This operation pops off a context, invokes a partial with that context,
// and pushes the result of the invocation back.
invokePartial: function(name, indent) {
- var params = [this.nameLookup('partials', name, 'partial'), "'" + indent + "'", "'" + name + "'", this.popStack(), this.popStack(), "helpers", "partials"];
+ var params = [],
+ options = this.setupParams(name, 1, params, false);
- if (this.options.data) {
- params.push("data");
- } else if (this.options.compat) {
- params.push('undefined');
+ if (indent) {
+ options.indent = JSON.stringify(indent);
}
+ options.helpers = 'helpers';
+ options.partials = 'partials';
+
+ params.unshift(this.nameLookup('partials', name, 'partial'));
+
if (this.options.compat) {
- params.push('depths');
+ options.depths = 'depths';
}
+ options = this.objectLiteral(options);
+ params.push(options);
this.push(this.source.functionCall('this.invokePartial', '', params));
},
@@ -881,7 +886,7 @@ JavaScriptCompiler.prototype = {
setupHelper: function(paramSize, name, blockHelper) {
var params = [],
- paramsInit = this.setupParams(name, paramSize, params, blockHelper);
+ paramsInit = this.setupHelperArgs(name, paramSize, params, blockHelper);
var foundHelper = this.nameLookup('helpers', name, 'helper');
return {
@@ -892,8 +897,8 @@ JavaScriptCompiler.prototype = {
};
},
- setupParams: function(helper, paramSize, params, useRegister) {
- var options = {}, contexts = [], types = [], ids = [], param, inverse, program;
+ setupParams: function(helper, paramSize, params) {
+ var options = {}, contexts = [], types = [], ids = [], param;
options.name = this.quotedString(helper);
options.hash = this.popStack();
@@ -906,8 +911,8 @@ JavaScriptCompiler.prototype = {
options.hashContexts = this.popStack();
}
- inverse = this.popStack();
- program = this.popStack();
+ var inverse = this.popStack(),
+ program = this.popStack();
// Avoid setting fn and inverse if neither are set. This allows
// helpers to do a check for `if (options.fn)`
@@ -943,7 +948,11 @@ JavaScriptCompiler.prototype = {
if (this.options.data) {
options.data = "data";
}
+ return options;
+ },
+ setupHelperArgs: function(helper, paramSize, params, useRegister) {
+ var options = this.setupParams(helper, paramSize, params, true);
options = this.objectLiteral(options);
if (useRegister) {
this.useRegister('options');