Added compiler cache and got rid of TODO file.

This commit is contained in:
Alan Johnson
2010-09-25 17:40:23 -04:00
parent 88d0ea90fc
commit f288a75e39
3 changed files with 19 additions and 29 deletions
-8
View File
@@ -1,8 +0,0 @@
* README
* Rationale (how it's different from mustache, and why)
* Refactor blocks/inverted sections so shared code isn't copied and pasted
* Add support for {{^}} when helperMissing is in play.
* Figure out how to allow detection of first/last/iteration when enumerating over arrays.
* Easier way to push context within a block helper, so that it can pass a built context to its function.
+18 -19
View File
@@ -1,20 +1,27 @@
var Handlebars = {
compilerCache: {},
compile: function(string) {
var fnBody = Handlebars.compileFunctionBody(string);
var fn = new Function("context", "fallback", "Handlebars", fnBody);
return function(context, fallback) { return fn(context, fallback, Handlebars); };
if (Handlebars.compilerCache[string] == null) {
var fnBody = Handlebars.compileFunctionBody(string);
var fn = new Function("context", "fallback", "Handlebars", fnBody);
Handlebars.compilerCache[string] =
function(context, fallback) { return fn(context, fallback, Handlebars); };
}
return Handlebars.compilerCache[string];
},
compileToString: function(string) {
var fnBody = Handlebars.compileFunctionBody(string);
return "function(context, fallback) { " + fnBody + "}";
},
compileFunctionBody: function(string) {
var compiler = new Handlebars.Compiler(string);
compiler.compile();
var compiler = new Handlebars.Compiler(string);
compiler.compile();
return "fallback = fallback || {}; var stack = [];" + compiler.fn;
return "fallback = fallback || {}; var stack = [];" + compiler.fn;
},
isFunction: function(fn) {
@@ -62,17 +69,11 @@ var Handlebars = {
});
},
compiledPartials: {},
compilePartial: function(partial) {
var compiled = Handlebars.compiledPartials[partial];
if (compiled == null) {
if (Handlebars.isFunction(partial)) {
compiled = partial;
} else {
compiled = Handlebars.compile(partial);
}
Handlebars.compiledPartials[partial] = compiled;
if (Handlebars.isFunction(partial)) {
compiled = partial;
} else {
compiled = Handlebars.compile(partial);
}
return compiled;
@@ -191,9 +192,7 @@ var Handlebars = {
out = out + Handlebars.helperMissing.not.call(arg, lookup, notFn);
}
}
return out;
},
+1 -2
View File
@@ -79,9 +79,8 @@ var data = {
]
}
var fn = Handlebars.compile(tmpl);
var test1 = function() {
fn(data, {partials: partials});
Handlebars.compile(tmpl)(data, {partials: partials});
}
var test2 = function() {
Mustache.to_html(tmpl, data, partials);