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