summaryrefslogtreecommitdiffstats
path: root/lib/handlebars/compiler/compiler.js
diff options
context:
space:
mode:
authorkpdecker <kpdecker@gmail.com>2011-07-31 21:44:16 -0500
committerkpdecker <kpdecker@gmail.com>2011-07-31 21:44:16 -0500
commitd6b97cf34da55e266aa064add08f6847a9031c17 (patch)
treeb3114ae3775b13b7d217430e4950623de4039bec /lib/handlebars/compiler/compiler.js
parent82e3344aedce805a029ab52573af4043f79a1ec3 (diff)
downloadhandlebars.js-d6b97cf34da55e266aa064add08f6847a9031c17.zip
handlebars.js-d6b97cf34da55e266aa064add08f6847a9031c17.tar.gz
handlebars.js-d6b97cf34da55e266aa064add08f6847a9031c17.tar.bz2
Do not buffer for simple programs (1 statement)
Diffstat (limited to 'lib/handlebars/compiler/compiler.js')
-rw-r--r--lib/handlebars/compiler/compiler.js26
1 files changed, 21 insertions, 5 deletions
diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js
index a887ba3..0679135 100644
--- a/lib/handlebars/compiler/compiler.js
+++ b/lib/handlebars/compiler/compiler.js
@@ -127,6 +127,7 @@ Handlebars.JavaScriptCompiler = function() {};
statement = statements[i];
this[statement.type](statement);
}
+ this.isSimple = l === 1;
this.depths.list = this.depths.list.sort(function(a, b) {
return a - b;
@@ -323,7 +324,11 @@ Handlebars.JavaScriptCompiler = function() {};
},
appendToBuffer: function(string) {
- return "buffer += " + string + ";";
+ if (this.environment.isSimple) {
+ return "return " + string + ";";
+ } else {
+ return "buffer += " + string + ";";
+ }
},
initializeBuffer: function() {
@@ -350,7 +355,7 @@ Handlebars.JavaScriptCompiler = function() {};
this.compileChildren(environment, options);
- var opcodes = environment.opcodes, opcode, name, declareName, declareVal;
+ var opcodes = environment.opcodes, opcode;
this.i = 0;
@@ -406,7 +411,11 @@ Handlebars.JavaScriptCompiler = function() {};
out.push('');
}
- out.push("var buffer = " + this.initializeBuffer());
+ if (!this.environment.isSimple) {
+ out.push(", buffer = " + this.initializeBuffer());
+ } else {
+ out.push("");
+ }
// track the last context pushed into place to allow skipping the
// getContext opcode when it would be a noop
@@ -432,14 +441,18 @@ Handlebars.JavaScriptCompiler = function() {};
}
}
- this.source[1] = this.source[1] + ";";
+ if (this.source[1]) {
+ this.source[1] = "var " + this.source[1].substring(2) + ";";
+ }
// Merge children
if (!this.isChild) {
this.source[1] += '\n' + this.context.programs.join('\n') + '\n';
}
- this.source.push("return buffer;");
+ if (!this.environment.isSimple) {
+ this.source.push("return buffer;");
+ }
var params = this.isChild ? ["depth0", "data"] : ["Handlebars", "depth0", "helpers", "partials", "data"];
@@ -467,6 +480,9 @@ Handlebars.JavaScriptCompiler = function() {};
append: function() {
var local = this.popStack();
this.source.push("if(" + local + " || " + local + " === 0) { " + this.appendToBuffer(local) + " }");
+ if (this.environment.isSimple) {
+ this.source.push("else { " + this.appendToBuffer("''") + " }");
+ }
},
appendEscaped: function() {