diff --git a/compat.hb b/compat.hb new file mode 100644 index 00000000..5dd9cfc1 --- /dev/null +++ b/compat.hb @@ -0,0 +1,8 @@ +This is the story of guys who work on a project +called {{project}}. Their names were {{#people}}{{firstName}} and {{/people}} +they both enjoyed working on {{project}}. + +{{#people}} +{{>personPet}} +{{/people}} + diff --git a/lib/compiler.js b/lib/compiler.js new file mode 100644 index 00000000..66d2bcf8 --- /dev/null +++ b/lib/compiler.js @@ -0,0 +1,11 @@ +require('./handlebars'); +var fs = require('fs'); + +process.argv.slice(2).forEach(function(val, index, array) { + var parts = val.split("=", 2); + + var tmpl = fs.readFileSync(parts[1], 'utf8'); + + console.log(parts[0] + " = " + Handlebars.compileToString(tmpl).toString()); +}); + diff --git a/lib/handlebars.js b/lib/handlebars.js index 58c84a96..eb64b6c3 100644 --- a/lib/handlebars.js +++ b/lib/handlebars.js @@ -1,8 +1,19 @@ Handlebars = { compile: function(string) { + var fnBody = Handlebars.compileFunctionBody(string); + return new Function("context", "fallback", fnBody); + }, + + compileToString: function(string) { + var fnBody = Handlebars.compileFunctionBody(string); + return "function(context, fallback) { " + fnBody + "}"; + }, + + compileFunctionBody: function(string) { var compiler = new Handlebars.Compiler(string); compiler.compile(); - return new Function("context", "fallback", "fallback = fallback || {}; var stack = [];" + compiler.fn); + + return "fallback = fallback || {}; var stack = [];" + compiler.fn; }, isFunction: function(fn) { diff --git a/test/compat.hbs b/test/compat.hbs new file mode 100644 index 00000000..5dd9cfc1 --- /dev/null +++ b/test/compat.hbs @@ -0,0 +1,8 @@ +This is the story of guys who work on a project +called {{project}}. Their names were {{#people}}{{firstName}} and {{/people}} +they both enjoyed working on {{project}}. + +{{#people}} +{{>personPet}} +{{/people}} +