2 Shameless port of a shameless port
3 @defunkt => @janl => @aq
5 See http://github.com/defunkt/mustache for more info.
11 mustache.js — Logic-less templates in JavaScript
13 See http://mustache.github.com/ for more info.
16 var Mustache = function() {
17 var Renderer = function() {};
19 Renderer.prototype = {
24 pragmas_implemented: {
25 "IMPLICIT-ITERATOR": true
29 render: function(template, context, partials, in_recursion) {
30 // reset buffer & set context
32 this.context = context;
33 this.buffer = []; // TODO: make this non-lazy
37 if(!this.includes("", template)) {
46 template = this.render_pragmas(template);
47 var html = this.render_section(template, context, partials);
49 return this.render_tags(html, context, partials, in_recursion);
52 this.render_tags(html, context, partials, in_recursion);
58 send: function(line) {
60 this.buffer.push(line);
67 render_pragmas: function(template) {
69 if(!this.includes("%", template)) {
74 var regex = new RegExp(this.otag + "%([\\w-]+) ?([\\w]+=[\\w]+)?" +
76 return template.replace(regex, function(match, pragma, options) {
77 if(!that.pragmas_implemented[pragma]) {
79 "This implementation of mustache doesn't understand the '" +
80 pragma + "' pragma"});
82 that.pragmas[pragma] = {};
84 var opts = options.split("=");
85 that.pragmas[pragma][opts[0]] = opts[1];
88 // ignore unknown pragmas silently
93 Tries to find a partial in the curent scope and render it
95 render_partial: function(name, context, partials) {
96 name = this.trim(name);
97 if(!partials || partials[name] === undefined) {
98 throw({message: "unknown_partial '" + name + "'"});
100 if(typeof(context[name]) != "object") {
101 return this.render(partials[name], context, partials, true);
103 return this.render(partials[name], context[name], partials, true);
107 Renders inverted (^) and normal (#) sections
109 render_section: function(template, context, partials) {
110 if(!this.includes("#", template) && !this.includes("^", template)) {
115 // CSW - Added "+?" so it finds the tighest bound, not the widest
116 var regex = new RegExp(this.otag + "(\\^|\\#)\\s*(.+)\\s*" + this.ctag +
117 "\n*([\\s\\S]+?)" + this.otag + "\\/\\s*\\2\\s*" + this.ctag +
120 // for each {{#foo}}{{/foo}} section do...
121 return template.replace(regex, function(match, type, name, content) {
122 var value = that.find(name, context);
123 if(type == "^") { // inverted section
124 if(!value || that.is_array(value) && value.length === 0) {
125 // false or empty list, render it
126 return that.render(content, context, partials, true);
130 } else if(type == "#") { // normal section
131 if(that.is_array(value)) { // Enumerable, Let's loop!
132 return that.map(value, function(row) {
133 return that.render(content, that.create_context(row),
136 } else if(that.is_object(value)) { // Object, Use it as subcontext!
137 return that.render(content, that.create_context(value),
139 } else if(typeof value === "function") {
140 // higher order section
141 return value.call(context, content, function(text) {
142 return that.render(text, context, partials, true);
144 } else if(value) { // boolean section
145 return that.render(content, context, partials, true);
154 Replace {{foo}} and friends with values from our view
156 render_tags: function(template, context, partials, in_recursion) {
160 var new_regex = function() {
161 return new RegExp(that.otag + "(=|!|>|\\{|%)?([^\\/#\\^]+?)\\1?" +
162 that.ctag + "+", "g");
165 var regex = new_regex();
166 var tag_replace_callback = function(match, operator, name) {
168 case "!": // ignore comments
170 case "=": // set new delimiters, rebuild the replace regexp
171 that.set_delimiters(name);
174 case ">": // render partial
175 return that.render_partial(name, context, partials);
176 case "{": // the triple mustache is unescaped
177 return that.find(name, context);
178 default: // escape the value
179 return that.escape(that.find(name, context));
182 var lines = template.split("\n");
183 for(var i = 0; i < lines.length; i++) {
184 lines[i] = lines[i].replace(regex, tag_replace_callback, this);
191 return lines.join("\n");
195 set_delimiters: function(delimiters) {
196 var dels = delimiters.split(" ");
197 this.otag = this.escape_regex(dels[0]);
198 this.ctag = this.escape_regex(dels[1]);
201 escape_regex: function(text) {
202 // thank you Simon Willison
203 if(!arguments.callee.sRE) {
205 '/', '.', '*', '+', '?', '|',
206 '(', ')', '[', ']', '{', '}', '\\'
208 arguments.callee.sRE = new RegExp(
209 '(\\' + specials.join('|\\') + ')', 'g'
212 return text.replace(arguments.callee.sRE, '\\$1');
216 find `name` in current `context`. That is find me a value
219 find: function(name, context) {
220 name = this.trim(name);
222 // Checks whether a value is thruthy or false or 0
223 function is_kinda_truthy(bool) {
224 return bool === false || bool === 0 || bool;
228 if(is_kinda_truthy(context[name])) {
229 value = context[name];
230 } else if(is_kinda_truthy(this.context[name])) {
231 value = this.context[name];
234 if(typeof value === "function") {
235 return value.apply(context);
237 if(value !== undefined) {
240 // silently ignore unkown variables
247 includes: function(needle, haystack) {
248 return haystack.indexOf(this.otag + needle) != -1;
252 Does away with nasty characters
254 escape: function(s) {
255 s = String(s === null ? "" : s);
256 return s.replace(/&(?!\w+;)|["<>\\]/g, function(s) {
258 case "&": return "&";
259 case "\\": return "\\\\";
260 case '"': return '\"';
261 case "<": return "<";
262 case ">": return ">";
268 // by @langalex, support for arrays of strings
269 create_context: function(_context) {
270 if(this.is_object(_context)) {
274 if(this.pragmas["IMPLICIT-ITERATOR"]) {
275 iterator = this.pragmas["IMPLICIT-ITERATOR"].iterator;
278 ctx[iterator] = _context;
283 is_object: function(a) {
284 return a && typeof a == "object";
287 is_array: function(a) {
288 return Object.prototype.toString.call(a) === '[object Array]';
292 Gets rid of leading and trailing whitespace
295 return s.replace(/^\s*|\s*$/g, "");
299 Why, why, why? Because IE. Cry, cry cry.
301 map: function(array, fn) {
302 if (typeof array.map == "function") {
303 return array.map(fn);
306 var l = array.length;
307 for(var i = 0; i < l; i++) {
308 r.push(fn(array[i]));
317 version: "0.3.1-dev",
320 Turns a template and view into HTML
322 to_html: function(template, view, partials, send_fun) {
323 var renderer = new Renderer();
325 renderer.send = send_fun;
327 renderer.render(template, view, partials);
329 return renderer.buffer.join("\n");
332 escape : function(text) {
333 return new Renderer().escape(text);
338 $.mustache = function(template, view, partials) {
339 return Mustache.to_html(template, view, partials);
342 $.mustache.escape = function(text) {
343 return Mustache.escape(text);