summaryrefslogtreecommitdiffstats
path: root/lib/handlebars/compiler.js
diff options
context:
space:
mode:
authorwycats <wycats@gmail.com>2010-12-18 23:27:34 -0800
committerwycats <wycats@gmail.com>2010-12-18 23:27:34 -0800
commitb418ef6eb23aa671b814a2c41163d043552b0ead (patch)
tree4e91caa83a4cccaf1b13fbb0c754b0a1699488a6 /lib/handlebars/compiler.js
parente15d675a64c7bf2c28fc52fb655705462ed60cbb (diff)
downloadhandlebars.js-b418ef6eb23aa671b814a2c41163d043552b0ead.zip
handlebars.js-b418ef6eb23aa671b814a2c41163d043552b0ead.tar.gz
handlebars.js-b418ef6eb23aa671b814a2c41163d043552b0ead.tar.bz2
Add optimized compiled version of handlebars, which should be significantly faster. Use Handlebars.VM.compile instead of Handlebars.compile to use the optimized version.
Major TODOS: * clean up a bunch of code duplication in the compiler * reorganize the compiler * add support for debug symbols which would make it possible to provide information about what part of the source caused a runtime error.
Diffstat (limited to 'lib/handlebars/compiler.js')
-rw-r--r--lib/handlebars/compiler.js23
1 files changed, 17 insertions, 6 deletions
diff --git a/lib/handlebars/compiler.js b/lib/handlebars/compiler.js
index 33e1fdf..96ae36a 100644
--- a/lib/handlebars/compiler.js
+++ b/lib/handlebars/compiler.js
@@ -1,7 +1,7 @@
var handlebars = require("handlebars/parser").parser;
// BEGIN(BROWSER)
-Handlebars = {};
+var Handlebars = {};
Handlebars.Parser = handlebars;
@@ -48,16 +48,27 @@ Handlebars.registerPartial = function(name, str) {
this.partials[name] = str;
};
-Handlebars.registerHelper('blockHelperMissing', function(context, fn) {
+Handlebars.registerHelper('blockHelperMissing', function(context, fn, inverse) {
+ inverse = inverse || function() {};
+
var ret = "";
+ var type = Object.prototype.toString.call(context);
+
+ if(type === "[object Function]") {
+ context = context();
+ }
if(context === true) {
return fn(this);
} else if(context === false || context == null) {
- return "";
- } else if(Object.prototype.toString.call(context) === "[object Array]") {
- for(var i=0, j=context.length; i<j; i++) {
- ret = ret + fn(context[i]);
+ return inverse(this);
+ } else if(type === "[object Array]") {
+ if(context.length > 0) {
+ for(var i=0, j=context.length; i<j; i++) {
+ ret = ret + fn(context[i]);
+ }
+ } else {
+ ret = inverse(this);
}
return ret;
} else {