From 26dfacd8beff5a9bc418491ad6bcf5336e20917f Mon Sep 17 00:00:00 2001 From: Michael Jackson Date: Sat, 22 Sep 2012 14:12:16 -0700 Subject: [PATCH] Add Mustache.compileTokens Fixes #258 --- mustache.js | 64 +++++++++++++++++++++++++-------------------- test/writer_test.js | 12 ++++++++- 2 files changed, 47 insertions(+), 29 deletions(-) diff --git a/mustache.js b/mustache.js index 981b88728..7165a8aba 100644 --- a/mustache.js +++ b/mustache.js @@ -193,41 +193,43 @@ var Mustache; }; Writer.prototype.compile = function (template, tags) { - return this._compile(template, tags); + var fn = this._cache[template]; + + if (!fn) { + var tokens = exports.parse(template, tags); + fn = this._cache[template] = this.compileTokens(tokens, template); + } + + return fn; }; Writer.prototype.compilePartial = function (name, template, tags) { - var fn = this._compile(template, tags); + var fn = this.compile(template, tags); this._partialCache[name] = fn; return fn; }; - Writer.prototype.render = function (template, view, partials) { - return this.compile(template)(view, partials); - }; - - Writer.prototype._compile = function (template, tags) { - if (!this._cache[template]) { - var tokens = exports.parse(template, tags); - var fn = compileTokens(tokens); + Writer.prototype.compileTokens = function (tokens, template) { + var fn = compileTokens(tokens); + var self = this; - var self = this; - this._cache[template] = function (view, partials) { - if (partials) { - if (typeof partials === "function") { - self._loadPartial = partials; - } else { - for (var name in partials) { - self.compilePartial(name, partials[name]); - } + return function (view, partials) { + if (partials) { + if (typeof partials === "function") { + self._loadPartial = partials; + } else { + for (var name in partials) { + self.compilePartial(name, partials[name]); } } + } - return fn(self, Context.make(view), template); - }; - } + return fn(self, Context.make(view), template); + }; + }; - return this._cache[template]; + Writer.prototype.render = function (template, view, partials) { + return this.compile(template)(view, partials); }; Writer.prototype._section = function (name, context, text, callback) { @@ -318,7 +320,7 @@ var Mustache; /** * Low-level function that compiles the given `tokens` into a function - * that accepts two arguments: a Context and a Writer. + * that accepts three arguments: a Writer, a Context, and the template. */ function compileTokens(tokens) { var subRenders = {}; @@ -334,7 +336,7 @@ var Mustache; return subRenders[i]; } - function renderFunction(writer, context, template) { + return function (writer, context, template) { var buffer = ""; var token, sectionText; @@ -365,9 +367,7 @@ var Mustache; } return buffer; - } - - return renderFunction; + }; } /** @@ -590,6 +590,14 @@ var Mustache; return _writer.compilePartial(name, template, tags); }; + /** + * Compiles the given array of tokens (the output of a parse) to a reusable + * function using the default writer. + */ + exports.compileTokens = function (tokens, template) { + return _writer.compileTokens(tokens, template); + }; + /** * Renders the `template` with the given `view` and `partials` using the * default writer. diff --git a/test/writer_test.js b/test/writer_test.js index f2253d58f..10a45872f 100644 --- a/test/writer_test.js +++ b/test/writer_test.js @@ -1,6 +1,7 @@ var assert = require("assert"); var vows = require("vows"); -var Writer = require("./../mustache").Writer; +var Mustache = require("./../mustache"); +var Writer = Mustache.Writer; vows.describe("Mustache.Writer").addBatch({ "A Writer": { @@ -29,6 +30,15 @@ vows.describe("Mustache.Writer").addBatch({ }); assert.equal(result, "partial two"); + }, + "can compile an array of tokens": function (writer) { + var template = "Hello {{name}}!"; + var tokens = Mustache.parse(template); + var render = writer.compileTokens(tokens, template); + + var result = render({ name: 'Michael' }); + + assert.equal(result, 'Hello Michael!'); } } }).export(module);