Skip to content

Commit

Permalink
Add Mustache.compileTokens
Browse files Browse the repository at this point in the history
Fixes #258
  • Loading branch information
mjackson committed Sep 22, 2012
1 parent 658f9e0 commit 26dfacd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 29 deletions.
64 changes: 36 additions & 28 deletions mustache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 = {};
Expand All @@ -334,7 +336,7 @@ var Mustache;
return subRenders[i];
}

function renderFunction(writer, context, template) {
return function (writer, context, template) {
var buffer = "";
var token, sectionText;

Expand Down Expand Up @@ -365,9 +367,7 @@ var Mustache;
}

return buffer;
}

return renderFunction;
};
}

/**
Expand Down Expand Up @@ -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.
Expand Down
12 changes: 11 additions & 1 deletion test/writer_test.js
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -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);

0 comments on commit 26dfacd

Please sign in to comment.