diff options
Diffstat (limited to 'lib/handlebars/compiler/javascript-compiler.js')
-rw-r--r-- | lib/handlebars/compiler/javascript-compiler.js | 29 |
1 files changed, 20 insertions, 9 deletions
diff --git a/lib/handlebars/compiler/javascript-compiler.js b/lib/handlebars/compiler/javascript-compiler.js index 0472dbb..f1b6d36 100644 --- a/lib/handlebars/compiler/javascript-compiler.js +++ b/lib/handlebars/compiler/javascript-compiler.js @@ -69,6 +69,8 @@ JavaScriptCompiler.prototype = { this.compileChildren(environment, options); + this.useDepths = this.useDepths || environment.depths.list.length; + var opcodes = environment.opcodes, opcode, i, @@ -115,6 +117,9 @@ JavaScriptCompiler.prototype = { if (this.options.data) { ret.useData = true; } + if (this.useDepths) { + ret.depths = true; + } if (!asObject) { ret.compiler = JSON.stringify(ret.compiler); @@ -151,8 +156,8 @@ JavaScriptCompiler.prototype = { var params = ["depth0", "helpers", "partials", "data"]; - for(var i=0, l=this.environment.depths.list.length; i<l; i++) { - params.push("depth" + this.environment.depths.list[i]); + if (this.useDepths) { + params.push('depths'); } // Perform a second pass over the output to merge content when possible @@ -668,6 +673,8 @@ JavaScriptCompiler.prototype = { child.name = 'program' + index; this.context.programs[index] = compiler.compile(child, options, this.context, !this.precompile); this.context.environments[index] = child; + + this.useDepths = this.useDepths || compiler.useDepths; } else { child.index = index; child.name = 'program' + index; @@ -689,17 +696,17 @@ JavaScriptCompiler.prototype = { } var child = this.environment.children[guid], - depths = child.depths.list, depth; + depths = child.depths.list, + useDepths = this.useDepths, + depth; var programParams = [child.index, 'data']; - for(var i=0, l = depths.length; i<l; i++) { - depth = depths[i]; - - programParams.push('depth' + (depth - 1)); + if (useDepths) { + programParams.push('depths'); } - return (depths.length === 0 ? 'this.program(' : 'this.programWithDepth(') + programParams.join(', ') + ')'; + return 'this.program(' + programParams.join(', ') + ')'; }, useRegister: function(name) { @@ -847,7 +854,11 @@ JavaScriptCompiler.prototype = { }, contextName: function(context) { - return 'depth' + context; + if (this.useDepths && context) { + return 'depths[' + context + ']'; + } else { + return 'depth' + context; + } }, quotedString: function(str) { |