|
311
|
1 |
/* |
|
|
2 |
mustache.js — Logic-less templates in JavaScript |
|
|
3 |
|
|
|
4 |
See http://mustache.github.com/ for more info. |
|
|
5 |
*/ |
|
|
6 |
|
|
|
7 |
var Mustache = function() { |
|
|
8 |
var regexCache = {}; |
|
|
9 |
var Renderer = function() {}; |
|
|
10 |
|
|
|
11 |
Renderer.prototype = { |
|
|
12 |
otag: "{{", |
|
|
13 |
ctag: "}}", |
|
|
14 |
pragmas: {}, |
|
|
15 |
buffer: [], |
|
|
16 |
pragmas_implemented: { |
|
|
17 |
"IMPLICIT-ITERATOR": true |
|
|
18 |
}, |
|
|
19 |
context: {}, |
|
|
20 |
|
|
|
21 |
render: function(template, context, partials, in_recursion) { |
|
|
22 |
// reset buffer & set context |
|
|
23 |
if(!in_recursion) { |
|
|
24 |
this.context = context; |
|
|
25 |
this.buffer = []; // TODO: make this non-lazy |
|
|
26 |
} |
|
|
27 |
|
|
|
28 |
// fail fast |
|
|
29 |
if(!this.includes("", template)) { |
|
|
30 |
if(in_recursion) { |
|
|
31 |
return template; |
|
|
32 |
} else { |
|
|
33 |
this.send(template); |
|
|
34 |
return; |
|
|
35 |
} |
|
|
36 |
} |
|
|
37 |
|
|
|
38 |
// get the pragmas together |
|
|
39 |
template = this.render_pragmas(template); |
|
|
40 |
|
|
|
41 |
// render the template |
|
|
42 |
var html = this.render_section(template, context, partials); |
|
|
43 |
|
|
|
44 |
// render_section did not find any sections, we still need to render the tags |
|
|
45 |
if (html === false) { |
|
|
46 |
html = this.render_tags(template, context, partials, in_recursion); |
|
|
47 |
} |
|
|
48 |
|
|
|
49 |
if (in_recursion) { |
|
|
50 |
return html; |
|
|
51 |
} else { |
|
|
52 |
this.sendLines(html); |
|
|
53 |
} |
|
|
54 |
}, |
|
|
55 |
|
|
|
56 |
/* |
|
|
57 |
Sends parsed lines |
|
|
58 |
*/ |
|
|
59 |
send: function(line) { |
|
|
60 |
if(line !== "") { |
|
|
61 |
this.buffer.push(line); |
|
|
62 |
} |
|
|
63 |
}, |
|
|
64 |
|
|
|
65 |
sendLines: function(text) { |
|
|
66 |
if (text) { |
|
|
67 |
var lines = text.split("\n"); |
|
|
68 |
for (var i = 0; i < lines.length; i++) { |
|
|
69 |
this.send(lines[i]); |
|
|
70 |
} |
|
|
71 |
} |
|
|
72 |
}, |
|
|
73 |
|
|
|
74 |
/* |
|
|
75 |
Looks for %PRAGMAS |
|
|
76 |
*/ |
|
|
77 |
render_pragmas: function(template) { |
|
|
78 |
// no pragmas |
|
|
79 |
if(!this.includes("%", template)) { |
|
|
80 |
return template; |
|
|
81 |
} |
|
|
82 |
|
|
|
83 |
var that = this; |
|
|
84 |
var regex = this.getCachedRegex("render_pragmas", function(otag, ctag) { |
|
|
85 |
return new RegExp(otag + "%([\\w-]+) ?([\\w]+=[\\w]+)?" + ctag, "g"); |
|
|
86 |
}); |
|
|
87 |
|
|
|
88 |
return template.replace(regex, function(match, pragma, options) { |
|
|
89 |
if(!that.pragmas_implemented[pragma]) { |
|
|
90 |
throw({message: |
|
|
91 |
"This implementation of mustache doesn't understand the '" + |
|
|
92 |
pragma + "' pragma"}); |
|
|
93 |
} |
|
|
94 |
that.pragmas[pragma] = {}; |
|
|
95 |
if(options) { |
|
|
96 |
var opts = options.split("="); |
|
|
97 |
that.pragmas[pragma][opts[0]] = opts[1]; |
|
|
98 |
} |
|
|
99 |
return ""; |
|
|
100 |
// ignore unknown pragmas silently |
|
|
101 |
}); |
|
|
102 |
}, |
|
|
103 |
|
|
|
104 |
/* |
|
|
105 |
Tries to find a partial in the curent scope and render it |
|
|
106 |
*/ |
|
|
107 |
render_partial: function(name, context, partials) { |
|
|
108 |
name = this.trim(name); |
|
|
109 |
if(!partials || partials[name] === undefined) { |
|
|
110 |
throw({message: "unknown_partial '" + name + "'"}); |
|
|
111 |
} |
|
|
112 |
if(typeof(context[name]) != "object") { |
|
|
113 |
return this.render(partials[name], context, partials, true); |
|
|
114 |
} |
|
|
115 |
return this.render(partials[name], context[name], partials, true); |
|
|
116 |
}, |
|
|
117 |
|
|
|
118 |
/* |
|
|
119 |
Renders inverted (^) and normal (#) sections |
|
|
120 |
*/ |
|
|
121 |
render_section: function(template, context, partials) { |
|
|
122 |
if(!this.includes("#", template) && !this.includes("^", template)) { |
|
|
123 |
// did not render anything, there were no sections |
|
|
124 |
return false; |
|
|
125 |
} |
|
|
126 |
|
|
|
127 |
var that = this; |
|
|
128 |
|
|
|
129 |
var regex = this.getCachedRegex("render_section", function(otag, ctag) { |
|
|
130 |
// This regex matches _the first_ section ({{#foo}}{{/foo}}), and captures the remainder |
|
|
131 |
return new RegExp( |
|
|
132 |
"^([\\s\\S]*?)" + // all the crap at the beginning that is not {{*}} ($1) |
|
|
133 |
|
|
|
134 |
otag + // {{ |
|
|
135 |
"(\\^|\\#)\\s*(.+)\\s*" + // #foo (# == $2, foo == $3) |
|
|
136 |
ctag + // }} |
|
|
137 |
|
|
|
138 |
"\n*([\\s\\S]*?)" + // between the tag ($2). leading newlines are dropped |
|
|
139 |
|
|
|
140 |
otag + // {{ |
|
|
141 |
"\\/\\s*\\3\\s*" + // /foo (backreference to the opening tag). |
|
|
142 |
ctag + // }} |
|
|
143 |
|
|
|
144 |
"\\s*([\\s\\S]*)$", // everything else in the string ($4). leading whitespace is dropped. |
|
|
145 |
|
|
|
146 |
"g"); |
|
|
147 |
}); |
|
|
148 |
|
|
|
149 |
|
|
|
150 |
// for each {{#foo}}{{/foo}} section do... |
|
|
151 |
return template.replace(regex, function(match, before, type, name, content, after) { |
|
|
152 |
// before contains only tags, no sections |
|
|
153 |
var renderedBefore = before ? that.render_tags(before, context, partials, true) : "", |
|
|
154 |
|
|
|
155 |
// after may contain both sections and tags, so use full rendering function |
|
|
156 |
renderedAfter = after ? that.render(after, context, partials, true) : "", |
|
|
157 |
|
|
|
158 |
// will be computed below |
|
|
159 |
renderedContent, |
|
|
160 |
|
|
|
161 |
value = that.find(name, context); |
|
|
162 |
|
|
|
163 |
if (type === "^") { // inverted section |
|
|
164 |
if (!value || that.is_array(value) && value.length === 0) { |
|
|
165 |
// false or empty list, render it |
|
|
166 |
renderedContent = that.render(content, context, partials, true); |
|
|
167 |
} else { |
|
|
168 |
renderedContent = ""; |
|
|
169 |
} |
|
|
170 |
} else if (type === "#") { // normal section |
|
|
171 |
if (that.is_array(value)) { // Enumerable, Let's loop! |
|
|
172 |
renderedContent = that.map(value, function(row) { |
|
|
173 |
return that.render(content, that.create_context(row), partials, true); |
|
|
174 |
}).join(""); |
|
|
175 |
} else if (that.is_object(value)) { // Object, Use it as subcontext! |
|
|
176 |
renderedContent = that.render(content, that.create_context(value), |
|
|
177 |
partials, true); |
|
|
178 |
} else if (typeof value === "function") { |
|
|
179 |
// higher order section |
|
|
180 |
renderedContent = value.call(context, content, function(text) { |
|
|
181 |
return that.render(text, context, partials, true); |
|
|
182 |
}); |
|
|
183 |
} else if (value) { // boolean section |
|
|
184 |
renderedContent = that.render(content, context, partials, true); |
|
|
185 |
} else { |
|
|
186 |
renderedContent = ""; |
|
|
187 |
} |
|
|
188 |
} |
|
|
189 |
|
|
|
190 |
return renderedBefore + renderedContent + renderedAfter; |
|
|
191 |
}); |
|
|
192 |
}, |
|
|
193 |
|
|
|
194 |
/* |
|
|
195 |
Replace {{foo}} and friends with values from our view |
|
|
196 |
*/ |
|
|
197 |
render_tags: function(template, context, partials, in_recursion) { |
|
|
198 |
// tit for tat |
|
|
199 |
var that = this; |
|
|
200 |
|
|
|
201 |
|
|
|
202 |
|
|
|
203 |
var new_regex = function() { |
|
|
204 |
return that.getCachedRegex("render_tags", function(otag, ctag) { |
|
|
205 |
return new RegExp(otag + "(=|!|>|\\{|%)?([^\\/#\\^]+?)\\1?" + ctag + "+", "g"); |
|
|
206 |
}); |
|
|
207 |
}; |
|
|
208 |
|
|
|
209 |
var regex = new_regex(); |
|
|
210 |
var tag_replace_callback = function(match, operator, name) { |
|
|
211 |
switch(operator) { |
|
|
212 |
case "!": // ignore comments |
|
|
213 |
return ""; |
|
|
214 |
case "=": // set new delimiters, rebuild the replace regexp |
|
|
215 |
that.set_delimiters(name); |
|
|
216 |
regex = new_regex(); |
|
|
217 |
return ""; |
|
|
218 |
case ">": // render partial |
|
|
219 |
return that.render_partial(name, context, partials); |
|
|
220 |
case "{": // the triple mustache is unescaped |
|
|
221 |
return that.find(name, context); |
|
|
222 |
default: // escape the value |
|
|
223 |
return that.escape(that.find(name, context)); |
|
|
224 |
} |
|
|
225 |
}; |
|
|
226 |
var lines = template.split("\n"); |
|
|
227 |
for(var i = 0; i < lines.length; i++) { |
|
|
228 |
lines[i] = lines[i].replace(regex, tag_replace_callback, this); |
|
|
229 |
if(!in_recursion) { |
|
|
230 |
this.send(lines[i]); |
|
|
231 |
} |
|
|
232 |
} |
|
|
233 |
|
|
|
234 |
if(in_recursion) { |
|
|
235 |
return lines.join("\n"); |
|
|
236 |
} |
|
|
237 |
}, |
|
|
238 |
|
|
|
239 |
set_delimiters: function(delimiters) { |
|
|
240 |
var dels = delimiters.split(" "); |
|
|
241 |
this.otag = this.escape_regex(dels[0]); |
|
|
242 |
this.ctag = this.escape_regex(dels[1]); |
|
|
243 |
}, |
|
|
244 |
|
|
|
245 |
escape_regex: function(text) { |
|
|
246 |
// thank you Simon Willison |
|
|
247 |
if(!arguments.callee.sRE) { |
|
|
248 |
var specials = [ |
|
|
249 |
'/', '.', '*', '+', '?', '|', |
|
|
250 |
'(', ')', '[', ']', '{', '}', '\\' |
|
|
251 |
]; |
|
|
252 |
arguments.callee.sRE = new RegExp( |
|
|
253 |
'(\\' + specials.join('|\\') + ')', 'g' |
|
|
254 |
); |
|
|
255 |
} |
|
|
256 |
return text.replace(arguments.callee.sRE, '\\$1'); |
|
|
257 |
}, |
|
|
258 |
|
|
|
259 |
/* |
|
|
260 |
find `name` in current `context`. That is find me a value |
|
|
261 |
from the view object |
|
|
262 |
*/ |
|
|
263 |
find: function(name, context) { |
|
|
264 |
name = this.trim(name); |
|
|
265 |
|
|
|
266 |
// Checks whether a value is thruthy or false or 0 |
|
|
267 |
function is_kinda_truthy(bool) { |
|
|
268 |
return bool === false || bool === 0 || bool; |
|
|
269 |
} |
|
|
270 |
|
|
|
271 |
var value; |
|
|
272 |
if(is_kinda_truthy(context[name])) { |
|
|
273 |
value = context[name]; |
|
|
274 |
} else if(is_kinda_truthy(this.context[name])) { |
|
|
275 |
value = this.context[name]; |
|
|
276 |
} |
|
|
277 |
|
|
|
278 |
if(typeof value === "function") { |
|
|
279 |
return value.apply(context); |
|
|
280 |
} |
|
|
281 |
if(value !== undefined) { |
|
|
282 |
return value; |
|
|
283 |
} |
|
|
284 |
// silently ignore unkown variables |
|
|
285 |
return ""; |
|
|
286 |
}, |
|
|
287 |
|
|
|
288 |
// Utility methods |
|
|
289 |
|
|
|
290 |
/* includes tag */ |
|
|
291 |
includes: function(needle, haystack) { |
|
|
292 |
return haystack.indexOf(this.otag + needle) != -1; |
|
|
293 |
}, |
|
|
294 |
|
|
|
295 |
/* |
|
|
296 |
Does away with nasty characters |
|
|
297 |
*/ |
|
|
298 |
escape: function(s) { |
|
|
299 |
s = String(s === null ? "" : s); |
|
|
300 |
return s.replace(/&(?!\w+;)|["'<>\\]/g, function(s) { |
|
|
301 |
switch(s) { |
|
|
302 |
case "&": return "&"; |
|
|
303 |
case '"': return '"'; |
|
|
304 |
case "'": return '''; |
|
|
305 |
case "<": return "<"; |
|
|
306 |
case ">": return ">"; |
|
|
307 |
default: return s; |
|
|
308 |
} |
|
|
309 |
}); |
|
|
310 |
}, |
|
|
311 |
|
|
|
312 |
// by @langalex, support for arrays of strings |
|
|
313 |
create_context: function(_context) { |
|
|
314 |
if(this.is_object(_context)) { |
|
|
315 |
return _context; |
|
|
316 |
} else { |
|
|
317 |
var iterator = "."; |
|
|
318 |
if(this.pragmas["IMPLICIT-ITERATOR"]) { |
|
|
319 |
iterator = this.pragmas["IMPLICIT-ITERATOR"].iterator; |
|
|
320 |
} |
|
|
321 |
var ctx = {}; |
|
|
322 |
ctx[iterator] = _context; |
|
|
323 |
return ctx; |
|
|
324 |
} |
|
|
325 |
}, |
|
|
326 |
|
|
|
327 |
is_object: function(a) { |
|
|
328 |
return a && typeof a == "object"; |
|
|
329 |
}, |
|
|
330 |
|
|
|
331 |
is_array: function(a) { |
|
|
332 |
return Object.prototype.toString.call(a) === '[object Array]'; |
|
|
333 |
}, |
|
|
334 |
|
|
|
335 |
/* |
|
|
336 |
Gets rid of leading and trailing whitespace |
|
|
337 |
*/ |
|
|
338 |
trim: function(s) { |
|
|
339 |
return s.replace(/^\s*|\s*$/g, ""); |
|
|
340 |
}, |
|
|
341 |
|
|
|
342 |
/* |
|
|
343 |
Why, why, why? Because IE. Cry, cry cry. |
|
|
344 |
*/ |
|
|
345 |
map: function(array, fn) { |
|
|
346 |
if (typeof array.map == "function") { |
|
|
347 |
return array.map(fn); |
|
|
348 |
} else { |
|
|
349 |
var r = []; |
|
|
350 |
var l = array.length; |
|
|
351 |
for(var i = 0; i < l; i++) { |
|
|
352 |
r.push(fn(array[i])); |
|
|
353 |
} |
|
|
354 |
return r; |
|
|
355 |
} |
|
|
356 |
}, |
|
|
357 |
|
|
|
358 |
getCachedRegex: function(name, generator) { |
|
|
359 |
var byOtag = regexCache[this.otag]; |
|
|
360 |
if (!byOtag) { |
|
|
361 |
byOtag = regexCache[this.otag] = {}; |
|
|
362 |
} |
|
|
363 |
|
|
|
364 |
var byCtag = byOtag[this.ctag]; |
|
|
365 |
if (!byCtag) { |
|
|
366 |
byCtag = byOtag[this.ctag] = {}; |
|
|
367 |
} |
|
|
368 |
|
|
|
369 |
var regex = byCtag[name]; |
|
|
370 |
if (!regex) { |
|
|
371 |
regex = byCtag[name] = generator(this.otag, this.ctag); |
|
|
372 |
} |
|
|
373 |
|
|
|
374 |
return regex; |
|
|
375 |
} |
|
|
376 |
}; |
|
|
377 |
|
|
|
378 |
return({ |
|
|
379 |
name: "mustache.js", |
|
|
380 |
version: "0.4.0-dev", |
|
|
381 |
|
|
|
382 |
/* |
|
|
383 |
Turns a template and view into HTML |
|
|
384 |
*/ |
|
|
385 |
to_html: function(template, view, partials, send_fun) { |
|
|
386 |
var renderer = new Renderer(); |
|
|
387 |
if(send_fun) { |
|
|
388 |
renderer.send = send_fun; |
|
|
389 |
} |
|
|
390 |
renderer.render(template, view || {}, partials); |
|
|
391 |
if(!send_fun) { |
|
|
392 |
return renderer.buffer.join("\n"); |
|
|
393 |
} |
|
|
394 |
} |
|
|
395 |
}); |
|
|
396 |
}(); |