@@ -193,41 +193,43 @@ var Mustache;
193
193
} ;
194
194
195
195
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 ;
197
204
} ;
198
205
199
206
Writer . prototype . compilePartial = function ( name , template , tags ) {
200
- var fn = this . _compile ( template , tags ) ;
207
+ var fn = this . compile ( template , tags ) ;
201
208
this . _partialCache [ name ] = fn ;
202
209
return fn ;
203
210
} ;
204
211
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 ;
213
215
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 ] ) ;
223
223
}
224
224
}
225
+ }
225
226
226
- return fn ( self , Context . make ( view ) , template ) ;
227
- } ;
228
- }
227
+ return fn ( self , Context . make ( view ) , template ) ;
228
+ } ;
229
+ } ;
229
230
230
- return this . _cache [ template ] ;
231
+ Writer . prototype . render = function ( template , view , partials ) {
232
+ return this . compile ( template ) ( view , partials ) ;
231
233
} ;
232
234
233
235
Writer . prototype . _section = function ( name , context , text , callback ) {
@@ -318,7 +320,7 @@ var Mustache;
318
320
319
321
/**
320
322
* 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 .
322
324
*/
323
325
function compileTokens ( tokens ) {
324
326
var subRenders = { } ;
@@ -334,7 +336,7 @@ var Mustache;
334
336
return subRenders [ i ] ;
335
337
}
336
338
337
- function renderFunction ( writer , context , template ) {
339
+ return function ( writer , context , template ) {
338
340
var buffer = "" ;
339
341
var token , sectionText ;
340
342
@@ -365,9 +367,7 @@ var Mustache;
365
367
}
366
368
367
369
return buffer ;
368
- }
369
-
370
- return renderFunction ;
370
+ } ;
371
371
}
372
372
373
373
/**
@@ -590,6 +590,14 @@ var Mustache;
590
590
return _writer . compilePartial ( name , template , tags ) ;
591
591
} ;
592
592
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
+
593
601
/**
594
602
* Renders the `template` with the given `view` and `partials` using the
595
603
* default writer.
0 commit comments