Skip to content

Commit 26dfacd

Browse files
committed
Add Mustache.compileTokens
Fixes #258
1 parent 658f9e0 commit 26dfacd

File tree

2 files changed

+47
-29
lines changed

2 files changed

+47
-29
lines changed

mustache.js

+36-28
Original file line numberDiff line numberDiff line change
@@ -193,41 +193,43 @@ var Mustache;
193193
};
194194

195195
Writer.prototype.compile = function (template, tags) {
196-
return this._compile(template, tags);
196+
var fn = this._cache[template];
197+
198+
if (!fn) {
199+
var tokens = exports.parse(template, tags);
200+
fn = this._cache[template] = this.compileTokens(tokens, template);
201+
}
202+
203+
return fn;
197204
};
198205

199206
Writer.prototype.compilePartial = function (name, template, tags) {
200-
var fn = this._compile(template, tags);
207+
var fn = this.compile(template, tags);
201208
this._partialCache[name] = fn;
202209
return fn;
203210
};
204211

205-
Writer.prototype.render = function (template, view, partials) {
206-
return this.compile(template)(view, partials);
207-
};
208-
209-
Writer.prototype._compile = function (template, tags) {
210-
if (!this._cache[template]) {
211-
var tokens = exports.parse(template, tags);
212-
var fn = compileTokens(tokens);
212+
Writer.prototype.compileTokens = function (tokens, template) {
213+
var fn = compileTokens(tokens);
214+
var self = this;
213215

214-
var self = this;
215-
this._cache[template] = function (view, partials) {
216-
if (partials) {
217-
if (typeof partials === "function") {
218-
self._loadPartial = partials;
219-
} else {
220-
for (var name in partials) {
221-
self.compilePartial(name, partials[name]);
222-
}
216+
return function (view, partials) {
217+
if (partials) {
218+
if (typeof partials === "function") {
219+
self._loadPartial = partials;
220+
} else {
221+
for (var name in partials) {
222+
self.compilePartial(name, partials[name]);
223223
}
224224
}
225+
}
225226

226-
return fn(self, Context.make(view), template);
227-
};
228-
}
227+
return fn(self, Context.make(view), template);
228+
};
229+
};
229230

230-
return this._cache[template];
231+
Writer.prototype.render = function (template, view, partials) {
232+
return this.compile(template)(view, partials);
231233
};
232234

233235
Writer.prototype._section = function (name, context, text, callback) {
@@ -318,7 +320,7 @@ var Mustache;
318320

319321
/**
320322
* Low-level function that compiles the given `tokens` into a function
321-
* that accepts two arguments: a Context and a Writer.
323+
* that accepts three arguments: a Writer, a Context, and the template.
322324
*/
323325
function compileTokens(tokens) {
324326
var subRenders = {};
@@ -334,7 +336,7 @@ var Mustache;
334336
return subRenders[i];
335337
}
336338

337-
function renderFunction(writer, context, template) {
339+
return function (writer, context, template) {
338340
var buffer = "";
339341
var token, sectionText;
340342

@@ -365,9 +367,7 @@ var Mustache;
365367
}
366368

367369
return buffer;
368-
}
369-
370-
return renderFunction;
370+
};
371371
}
372372

373373
/**
@@ -590,6 +590,14 @@ var Mustache;
590590
return _writer.compilePartial(name, template, tags);
591591
};
592592

593+
/**
594+
* Compiles the given array of tokens (the output of a parse) to a reusable
595+
* function using the default writer.
596+
*/
597+
exports.compileTokens = function (tokens, template) {
598+
return _writer.compileTokens(tokens, template);
599+
};
600+
593601
/**
594602
* Renders the `template` with the given `view` and `partials` using the
595603
* default writer.

test/writer_test.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var assert = require("assert");
22
var vows = require("vows");
3-
var Writer = require("./../mustache").Writer;
3+
var Mustache = require("./../mustache");
4+
var Writer = Mustache.Writer;
45

56
vows.describe("Mustache.Writer").addBatch({
67
"A Writer": {
@@ -29,6 +30,15 @@ vows.describe("Mustache.Writer").addBatch({
2930
});
3031

3132
assert.equal(result, "partial two");
33+
},
34+
"can compile an array of tokens": function (writer) {
35+
var template = "Hello {{name}}!";
36+
var tokens = Mustache.parse(template);
37+
var render = writer.compileTokens(tokens, template);
38+
39+
var result = render({ name: 'Michael' });
40+
41+
assert.equal(result, 'Hello Michael!');
3242
}
3343
}
3444
}).export(module);

0 commit comments

Comments
 (0)