4
4
See http://mustache.github.com/ for more info.
5
5
*/
6
6
7
- var Mustache = function ( ) {
7
+ var Mustache = function ( ) {
8
8
var _toString = Object . prototype . toString ;
9
9
10
10
Array . isArray = Array . isArray || function ( obj ) {
@@ -48,16 +48,16 @@ var Mustache = function() {
48
48
} ,
49
49
context : { } ,
50
50
51
- render : function ( template , context , partials , in_recursion ) {
51
+ render : function ( template , context , partials , in_recursion ) {
52
52
// reset buffer & set context
53
- if ( ! in_recursion ) {
53
+ if ( ! in_recursion ) {
54
54
this . context = context ;
55
55
this . buffer = [ ] ; // TODO: make this non-lazy
56
56
}
57
57
58
58
// fail fast
59
- if ( ! this . includes ( "" , template ) ) {
60
- if ( in_recursion ) {
59
+ if ( ! this . includes ( "" , template ) ) {
60
+ if ( in_recursion ) {
61
61
return template ;
62
62
} else {
63
63
this . send ( template ) ;
@@ -86,13 +86,13 @@ var Mustache = function() {
86
86
/*
87
87
Sends parsed lines
88
88
*/
89
- send : function ( line ) {
90
- if ( line !== "" ) {
89
+ send : function ( line ) {
90
+ if ( line !== "" ) {
91
91
this . buffer . push ( line ) ;
92
92
}
93
93
} ,
94
94
95
- sendLines : function ( text ) {
95
+ sendLines : function ( text ) {
96
96
if ( text ) {
97
97
var lines = text . split ( "\n" ) ;
98
98
for ( var i = 0 ; i < lines . length ; i ++ ) {
@@ -104,25 +104,25 @@ var Mustache = function() {
104
104
/*
105
105
Looks for %PRAGMAS
106
106
*/
107
- render_pragmas : function ( template ) {
107
+ render_pragmas : function ( template ) {
108
108
// no pragmas
109
- if ( ! this . includes ( "%" , template ) ) {
109
+ if ( ! this . includes ( "%" , template ) ) {
110
110
return template ;
111
111
}
112
112
113
113
var that = this ;
114
- var regex = this . getCachedRegex ( "render_pragmas" , function ( otag , ctag ) {
114
+ var regex = this . getCachedRegex ( "render_pragmas" , function ( otag , ctag ) {
115
115
return new RegExp ( otag + "%([\\w-]+) ?([\\w]+=[\\w]+)?" + ctag , "g" ) ;
116
116
} ) ;
117
117
118
- return template . replace ( regex , function ( match , pragma , options ) {
119
- if ( ! that . pragmas_implemented [ pragma ] ) {
118
+ return template . replace ( regex , function ( match , pragma , options ) {
119
+ if ( ! that . pragmas_implemented [ pragma ] ) {
120
120
throw ( { message :
121
121
"This implementation of mustache doesn't understand the '" +
122
122
pragma + "' pragma" } ) ;
123
123
}
124
124
that . pragmas [ pragma ] = { } ;
125
- if ( options ) {
125
+ if ( options ) {
126
126
var opts = options . split ( "=" ) ;
127
127
that . pragmas [ pragma ] [ opts [ 0 ] ] = opts [ 1 ] ;
128
128
}
@@ -134,12 +134,12 @@ var Mustache = function() {
134
134
/*
135
135
Tries to find a partial in the curent scope and render it
136
136
*/
137
- render_partial : function ( name , context , partials ) {
137
+ render_partial : function ( name , context , partials ) {
138
138
name = trim ( name ) ;
139
- if ( ! partials || partials [ name ] === undefined ) {
139
+ if ( ! partials || partials [ name ] === undefined ) {
140
140
throw ( { message : "unknown_partial '" + name + "'" } ) ;
141
141
}
142
- if ( typeof ( context [ name ] ) != "object" ) {
142
+ if ( typeof ( context [ name ] ) != "object" ) {
143
143
return this . render ( partials [ name ] , context , partials , true ) ;
144
144
}
145
145
return this . render ( partials [ name ] , context [ name ] , partials , true ) ;
@@ -148,15 +148,15 @@ var Mustache = function() {
148
148
/*
149
149
Renders inverted (^) and normal (#) sections
150
150
*/
151
- render_section : function ( template , context , partials ) {
152
- if ( ! this . includes ( "#" , template ) && ! this . includes ( "^" , template ) ) {
151
+ render_section : function ( template , context , partials ) {
152
+ if ( ! this . includes ( "#" , template ) && ! this . includes ( "^" , template ) ) {
153
153
// did not render anything, there were no sections
154
154
return false ;
155
155
}
156
156
157
157
var that = this ;
158
158
159
- var regex = this . getCachedRegex ( "render_section" , function ( otag , ctag ) {
159
+ var regex = this . getCachedRegex ( "render_section" , function ( otag , ctag ) {
160
160
// This regex matches _the first_ section ({{#foo}}{{/foo}}), and captures the remainder
161
161
return new RegExp (
162
162
"^([\\s\\S]*?)" + // all the crap at the beginning that is not {{*}} ($1)
@@ -178,7 +178,7 @@ var Mustache = function() {
178
178
179
179
180
180
// for each {{#foo}}{{/foo}} section do...
181
- return template . replace ( regex , function ( match , before , type , name , content , after ) {
181
+ return template . replace ( regex , function ( match , before , type , name , content , after ) {
182
182
// before contains only tags, no sections
183
183
var renderedBefore = before ? that . render_tags ( before , context , partials , true ) : "" ,
184
184
@@ -199,15 +199,15 @@ var Mustache = function() {
199
199
}
200
200
} else if ( type === "#" ) { // normal section
201
201
if ( Array . isArray ( value ) ) { // Enumerable, Let's loop!
202
- renderedContent = that . map ( value , function ( row ) {
202
+ renderedContent = that . map ( value , function ( row ) {
203
203
return that . render ( content , that . create_context ( row ) , partials , true ) ;
204
204
} ) . join ( "" ) ;
205
205
} else if ( that . is_object ( value ) ) { // Object, Use it as subcontext!
206
206
renderedContent = that . render ( content , that . create_context ( value ) ,
207
207
partials , true ) ;
208
208
} else if ( typeof value === "function" ) {
209
209
// higher order section
210
- renderedContent = value . call ( context , content , function ( text ) {
210
+ renderedContent = value . call ( context , content , function ( text ) {
211
211
return that . render ( text , context , partials , true ) ;
212
212
} ) ;
213
213
} else if ( value ) { // boolean section
@@ -224,20 +224,20 @@ var Mustache = function() {
224
224
/*
225
225
Replace {{foo}} and friends with values from our view
226
226
*/
227
- render_tags : function ( template , context , partials , in_recursion ) {
227
+ render_tags : function ( template , context , partials , in_recursion ) {
228
228
// tit for tat
229
229
var that = this ;
230
230
231
231
232
232
233
- var new_regex = function ( ) {
234
- return that . getCachedRegex ( "render_tags" , function ( otag , ctag ) {
233
+ var new_regex = function ( ) {
234
+ return that . getCachedRegex ( "render_tags" , function ( otag , ctag ) {
235
235
return new RegExp ( otag + "(=|!|>|\\{|%)?([^\\/#\\^]+?)\\1?" + ctag + "+" , "g" ) ;
236
236
} ) ;
237
237
} ;
238
238
239
239
var regex = new_regex ( ) ;
240
- var tag_replace_callback = function ( match , operator , name ) {
240
+ var tag_replace_callback = function ( match , operator , name ) {
241
241
switch ( operator ) {
242
242
case "!" : // ignore comments
243
243
return "" ;
@@ -256,25 +256,25 @@ var Mustache = function() {
256
256
var lines = template . split ( "\n" ) ;
257
257
for ( var i = 0 ; i < lines . length ; i ++ ) {
258
258
lines [ i ] = lines [ i ] . replace ( regex , tag_replace_callback , this ) ;
259
- if ( ! in_recursion ) {
259
+ if ( ! in_recursion ) {
260
260
this . send ( lines [ i ] ) ;
261
261
}
262
262
}
263
263
264
- if ( in_recursion ) {
264
+ if ( in_recursion ) {
265
265
return lines . join ( "\n" ) ;
266
266
}
267
267
} ,
268
268
269
- set_delimiters : function ( delimiters ) {
269
+ set_delimiters : function ( delimiters ) {
270
270
var dels = delimiters . split ( " " ) ;
271
271
this . otag = this . escape_regex ( dels [ 0 ] ) ;
272
272
this . ctag = this . escape_regex ( dels [ 1 ] ) ;
273
273
} ,
274
274
275
- escape_regex : function ( text ) {
275
+ escape_regex : function ( text ) {
276
276
// thank you Simon Willison
277
- if ( ! arguments . callee . sRE ) {
277
+ if ( ! arguments . callee . sRE ) {
278
278
var specials = [
279
279
'/' , '.' , '*' , '+' , '?' , '|' ,
280
280
'(' , ')' , '[' , ']' , '{' , '}' , '\\'
@@ -290,7 +290,7 @@ var Mustache = function() {
290
290
find `name` in current `context`. That is find me a value
291
291
from the view object
292
292
*/
293
- find : function ( name , context ) {
293
+ find : function ( name , context ) {
294
294
name = trim ( name ) ;
295
295
296
296
// Checks whether a value is thruthy or false or 0
@@ -301,41 +301,40 @@ var Mustache = function() {
301
301
var value ;
302
302
303
303
// check for dot notation eg. foo.bar
304
- if ( name . match ( / ( [ a - z _ ] + ) \. / ig) ) {
304
+ if ( name . match ( / ( [ a - z _ ] + ) \. / ig) ) {
305
305
var childValue = this . walk_context ( name , context ) ;
306
- if ( is_kinda_truthy ( childValue ) ) {
306
+ if ( is_kinda_truthy ( childValue ) ) {
307
307
value = childValue ;
308
308
}
309
- }
310
- else {
311
- if ( is_kinda_truthy ( context [ name ] ) ) {
309
+ } else {
310
+ if ( is_kinda_truthy ( context [ name ] ) ) {
312
311
value = context [ name ] ;
313
- } else if ( is_kinda_truthy ( this . context [ name ] ) ) {
312
+ } else if ( is_kinda_truthy ( this . context [ name ] ) ) {
314
313
value = this . context [ name ] ;
315
314
}
316
315
}
317
316
318
- if ( typeof value === "function" ) {
317
+ if ( typeof value === "function" ) {
319
318
return value . apply ( context ) ;
320
319
}
321
- if ( value !== undefined ) {
320
+ if ( value !== undefined ) {
322
321
return value ;
323
322
}
324
323
// silently ignore unkown variables
325
324
return "" ;
326
325
} ,
327
326
328
- walk_context : function ( name , context ) {
327
+ walk_context : function ( name , context ) {
329
328
var path = name . split ( '.' ) ;
330
329
// if the var doesn't exist in current context, check the top level context
331
330
var value_context = ( context [ path [ 0 ] ] != undefined ) ? context : this . context ;
332
331
var value = value_context [ path . shift ( ) ] ;
333
- while ( value != undefined && path . length > 0 ) {
332
+ while ( value != undefined && path . length > 0 ) {
334
333
value_context = value ;
335
334
value = value [ path . shift ( ) ] ;
336
335
}
337
336
// if the value is a function, call it, binding the correct context
338
- if ( typeof value === "function" ) {
337
+ if ( typeof value === "function" ) {
339
338
return value . apply ( value_context ) ;
340
339
}
341
340
return value ;
@@ -344,16 +343,16 @@ var Mustache = function() {
344
343
// Utility methods
345
344
346
345
/* includes tag */
347
- includes : function ( needle , haystack ) {
346
+ includes : function ( needle , haystack ) {
348
347
return haystack . indexOf ( this . otag + needle ) != - 1 ;
349
348
} ,
350
349
351
350
/*
352
351
Does away with nasty characters
353
352
*/
354
- escape : function ( s ) {
353
+ escape : function ( s ) {
355
354
s = String ( s === null ? "" : s ) ;
356
- return s . replace ( / & (? ! \w + ; ) | [ " ' < > \\ ] / g, function ( s ) {
355
+ return s . replace ( / & (? ! \w + ; ) | [ " ' < > \\ ] / g, function ( s ) {
357
356
switch ( s ) {
358
357
case "&" : return "&" ;
359
358
case '"' : return '"' ;
@@ -366,12 +365,12 @@ var Mustache = function() {
366
365
} ,
367
366
368
367
// by @langalex , support for arrays of strings
369
- create_context : function ( _context ) {
370
- if ( this . is_object ( _context ) ) {
368
+ create_context : function ( _context ) {
369
+ if ( this . is_object ( _context ) ) {
371
370
return _context ;
372
371
} else {
373
372
var iterator = "." ;
374
- if ( this . pragmas [ "IMPLICIT-ITERATOR" ] ) {
373
+ if ( this . pragmas [ "IMPLICIT-ITERATOR" ] ) {
375
374
iterator = this . pragmas [ "IMPLICIT-ITERATOR" ] . iterator ;
376
375
}
377
376
var ctx = { } ;
@@ -380,14 +379,14 @@ var Mustache = function() {
380
379
}
381
380
} ,
382
381
383
- is_object : function ( a ) {
382
+ is_object : function ( a ) {
384
383
return a && typeof a == "object" ;
385
384
} ,
386
385
387
386
/*
388
387
Why, why, why? Because IE. Cry, cry cry.
389
388
*/
390
- map : function ( array , fn ) {
389
+ map : function ( array , fn ) {
391
390
if ( typeof array . map == "function" ) {
392
391
return array . map ( fn ) ;
393
392
} else {
@@ -400,7 +399,7 @@ var Mustache = function() {
400
399
}
401
400
} ,
402
401
403
- getCachedRegex : function ( name , generator ) {
402
+ getCachedRegex : function ( name , generator ) {
404
403
var byOtag = regexCache [ this . otag ] ;
405
404
if ( ! byOtag ) {
406
405
byOtag = regexCache [ this . otag ] = { } ;
@@ -427,13 +426,13 @@ var Mustache = function() {
427
426
/*
428
427
Turns a template and view into HTML
429
428
*/
430
- to_html : function ( template , view , partials , send_fun ) {
429
+ to_html : function ( template , view , partials , send_fun ) {
431
430
var renderer = new Renderer ( ) ;
432
- if ( send_fun ) {
431
+ if ( send_fun ) {
433
432
renderer . send = send_fun ;
434
433
}
435
434
renderer . render ( template , view || { } , partials ) ;
436
- if ( ! send_fun ) {
435
+ if ( ! send_fun ) {
437
436
return renderer . buffer . join ( "\n" ) ;
438
437
}
439
438
}
0 commit comments