2 * CommonJS-compatible mustache.js module
4 * See http://github.com/janl/mustache.js for more info.
8 mustache.js — Logic-less templates in JavaScript
10 See http://mustache.github.com/ for more info.
13 var Mustache = function() {
14 var Renderer = function() {};
16 Renderer.prototype = {
21 pragmas_implemented: {
22 "IMPLICIT-ITERATOR": true
26 render: function(template, context, partials, in_recursion) {
27 // reset buffer & set context
29 this.context = context;
30 this.buffer = []; // TODO: make this non-lazy
34 if(!this.includes("", template)) {
43 template = this.render_pragmas(template);
44 var html = this.render_section(template, context, partials);
46 return this.render_tags(html, context, partials, in_recursion);
49 this.render_tags(html, context, partials, in_recursion);
55 send: function(line) {
57 this.buffer.push(line);
64 render_pragmas: function(template) {
66 if(!this.includes("%", template)) {
71 var regex = new RegExp(this.otag + "%([\\w-]+) ?([\\w]+=[\\w]+)?" +
73 return template.replace(regex, function(match, pragma, options) {
74 if(!that.pragmas_implemented[pragma]) {
76 "This implementation of mustache doesn't understand the '" +
77 pragma + "' pragma"});
79 that.pragmas[pragma] = {};
81 var opts = options.split("=");
82 that.pragmas[pragma][opts[0]] = opts[1];
85 // ignore unknown pragmas silently
90 Tries to find a partial in the curent scope and render it
92 render_partial: function(name, context, partials) {
93 name = this.trim(name);
94 if(!partials || partials[name] === undefined) {
95 throw({message: "unknown_partial '" + name + "'"});
97 if(typeof(context[name]) != "object") {
98 return this.render(partials[name], context, partials, true);
100 return this.render(partials[name], context[name], partials, true);
104 Renders inverted (^) and normal (#) sections
106 render_section: function(template, context, partials) {
107 if(!this.includes("#", template) && !this.includes("^", template)) {
112 // CSW - Added "+?" so it finds the tighest bound, not the widest
113 var regex = new RegExp(this.otag + "(\\^|\\#)\\s*(.+)\\s*" + this.ctag +
114 "\n*([\\s\\S]+?)" + this.otag + "\\/\\s*\\2\\s*" + this.ctag +
117 // for each {{#foo}}{{/foo}} section do...
118 return template.replace(regex, function(match, type, name, content) {
119 var value = that.find(name, context);
120 if(type == "^") { // inverted section
121 if(!value || that.is_array(value) && value.length === 0) {
122 // false or empty list, render it
123 return that.render(content, context, partials, true);
127 } else if(type == "#") { // normal section
128 if(that.is_array(value)) { // Enumerable, Let's loop!
129 return that.map(value, function(row) {
130 return that.render(content, that.create_context(row),
133 } else if(that.is_object(value)) { // Object, Use it as subcontext!
134 return that.render(content, that.create_context(value),
136 } else if(typeof value === "function") {
137 // higher order section
138 return value.call(context, content, function(text) {
139 return that.render(text, context, partials, true);
141 } else if(value) { // boolean section
142 return that.render(content, context, partials, true);
151 Replace {{foo}} and friends with values from our view
153 render_tags: function(template, context, partials, in_recursion) {
157 var new_regex = function() {
158 return new RegExp(that.otag + "(=|!|>|\\{|%)?([^\\/#\\^]+?)\\1?" +
159 that.ctag + "+", "g");
162 var regex = new_regex();
163 var tag_replace_callback = function(match, operator, name) {
165 case "!": // ignore comments
167 case "=": // set new delimiters, rebuild the replace regexp
168 that.set_delimiters(name);
171 case ">": // render partial
172 return that.render_partial(name, context, partials);
173 case "{": // the triple mustache is unescaped
174 return that.find(name, context);
175 default: // escape the value
176 return that.escape(that.find(name, context));
179 var lines = template.split("\n");
180 for(var i = 0; i < lines.length; i++) {
181 lines[i] = lines[i].replace(regex, tag_replace_callback, this);
188 return lines.join("\n");
192 set_delimiters: function(delimiters) {
193 var dels = delimiters.split(" ");
194 this.otag = this.escape_regex(dels[0]);
195 this.ctag = this.escape_regex(dels[1]);
198 escape_regex: function(text) {
199 // thank you Simon Willison
200 if(!arguments.callee.sRE) {
202 '/', '.', '*', '+', '?', '|',
203 '(', ')', '[', ']', '{', '}', '\\'
205 arguments.callee.sRE = new RegExp(
206 '(\\' + specials.join('|\\') + ')', 'g'
209 return text.replace(arguments.callee.sRE, '\\$1');
213 find `name` in current `context`. That is find me a value
216 find: function(name, context) {
217 name = this.trim(name);
219 // Checks whether a value is thruthy or false or 0
220 function is_kinda_truthy(bool) {
221 return bool === false || bool === 0 || bool;
225 if(is_kinda_truthy(context[name])) {
226 value = context[name];
227 } else if(is_kinda_truthy(this.context[name])) {
228 value = this.context[name];
231 if(typeof value === "function") {
232 return value.apply(context);
234 if(value !== undefined) {
237 // silently ignore unkown variables
244 includes: function(needle, haystack) {
245 return haystack.indexOf(this.otag + needle) != -1;
249 Does away with nasty characters
251 escape: function(s) {
252 s = String(s === null ? "" : s);
253 return s.replace(/&(?!\w+;)|["<>\\]/g, function(s) {
255 case "&": return "&";
256 case "\\": return "\\\\";
257 case '"': return '\"';
258 case "<": return "<";
259 case ">": return ">";
265 // by @langalex, support for arrays of strings
266 create_context: function(_context) {
267 if(this.is_object(_context)) {
271 if(this.pragmas["IMPLICIT-ITERATOR"]) {
272 iterator = this.pragmas["IMPLICIT-ITERATOR"].iterator;
275 ctx[iterator] = _context;
280 is_object: function(a) {
281 return a && typeof a == "object";
284 is_array: function(a) {
285 return Object.prototype.toString.call(a) === '[object Array]';
289 Gets rid of leading and trailing whitespace
292 return s.replace(/^\s*|\s*$/g, "");
296 Why, why, why? Because IE. Cry, cry cry.
298 map: function(array, fn) {
299 if (typeof array.map == "function") {
300 return array.map(fn);
303 var l = array.length;
304 for(var i = 0; i < l; i++) {
305 r.push(fn(array[i]));
314 version: "0.3.1-dev",
317 Turns a template and view into HTML
319 to_html: function(template, view, partials, send_fun) {
320 var renderer = new Renderer();
322 renderer.send = send_fun;
324 renderer.render(template, view, partials);
326 return renderer.buffer.join("\n");
329 escape : function(text) {
330 return new Renderer().escape(text);
335 exports.name = Mustache.name;
336 exports.version = Mustache.version;
338 exports.to_html = Mustache.to_html;
339 exports.escape = Mustache.escape;