2 Shameless port of http://github.com/defunkt/mustache
3 by Jan Lehnardt <jan@apache.org>,
4 Alexander Lang <alex@upstream-berlin.com>,
5 Sebastian Cohnen <sebastian.cohnen@googlemail.com>
7 Thanks @defunkt for the awesome code.
9 See http://github.com/defunkt/mustache for more info.
12 var Mustache = function() {
13 var Renderer = function() {};
15 Renderer.prototype = {
20 pragmas_parsed: false,
22 render: function(template, context, partials, in_recursion) {
24 if(template.indexOf(this.otag) == -1) {
36 if(!this.pragmas_parsed) {
37 template = this.render_pragmas(template);
39 var html = this.render_section(template, context, partials);
41 return this.render_tags(html, context, partials, in_recursion);
44 this.render_tags(html, context, partials, in_recursion);
50 send: function(line) {
52 this.buffer.push(line);
59 render_pragmas: function(template) {
60 this.pragmas_parsed = true;
62 if(template.indexOf(this.otag + "%") == -1) {
67 var regex = new RegExp(this.otag + "%([\\w_-]+) ?([\\w]+=[\\w]+)?"
69 return template.replace(regex, function(match, pragma, options) {
70 that.pragmas[pragma] = {};
72 var opts = options.split("=");
73 that.pragmas[pragma][opts[0]] = opts[1];
76 // ignore unknown pragmas silently
81 Tries to find a partial in the global scope and render it
83 render_partial: function(name, context, partials) {
84 if(typeof(context[name]) != "object") {
85 throw({message: "subcontext for '" + name + "' is not an object"});
87 if(!partials || !partials[name]) {
88 throw({message: "unknown_partial '" + name + "'"});
90 return this.render(partials[name], context[name], partials, true);
94 Renders boolean and enumerable sections
96 render_section: function(template, context, partials) {
97 if(template.indexOf(this.otag + "#") == -1) {
101 // CSW - Added "+?" so it finds the tighest bound, not the widest
102 var regex = new RegExp(this.otag + "\\#(.+)" + this.ctag +
103 "\\s*([\\s\\S]+?)" + this.otag + "\\/\\1" + this.ctag + "\\s*", "mg");
105 // for each {{#foo}}{{/foo}} section do...
106 return template.replace(regex, function(match, name, content) {
107 var value = that.find(name, context);
108 if(that.is_array(value)) { // Enumerable, Let's loop!
109 return that.map(value, function(row) {
110 return that.render(content, that.merge(context,
111 that.create_context(row)), partials, true);
113 } else if (that.is_iterator(value)) {
116 while (row = value()) {
117 result.push(that.render(content, that.merge(context,
118 that.create_context(row)), partials, true));
119 } // fuck buffering, works for now though.
120 return result.join('');
121 } else if(value) { // boolean section
122 return that.render(content, context, partials, true);
130 Replace {{foo}} and friends with values from our view
132 render_tags: function(template, context, partials, in_recursion) {
136 var new_regex = function() {
137 return new RegExp(that.otag + "(=|!|>|\\{|%)?([^\/#]+?)\\1?" +
138 that.ctag + "+", "g");
141 var regex = new_regex();
142 var lines = template.split("\n");
143 for (var i=0; i < lines.length; i++) {
144 lines[i] = lines[i].replace(regex, function(match, operator, name) {
146 case "!": // ignore comments
148 case "=": // set new delimiters, rebuild the replace regexp
149 that.set_delimiters(name);
152 case ">": // render partial
153 return that.render_partial(name, context, partials);
154 case "{": // the triple mustache is unescaped
155 return that.find(name, context);
156 default: // escape the value
157 return that.escape(that.find(name, context));
164 return lines.join("\n");
167 set_delimiters: function(delimiters) {
168 var dels = delimiters.split(" ");
169 this.otag = this.escape_regex(dels[0]);
170 this.ctag = this.escape_regex(dels[1]);
173 escape_regex: function(text) {
174 // thank you Simon Willison
175 if(!arguments.callee.sRE) {
177 '/', '.', '*', '+', '?', '|',
178 '(', ')', '[', ']', '{', '}', '\\'
180 arguments.callee.sRE = new RegExp(
181 '(\\' + specials.join('|\\') + ')', 'g'
184 return text.replace(arguments.callee.sRE, '\\$1');
188 find `name` in current `context`. That is find me a value
191 find: function(name, context) {
192 name = this.trim(name);
193 if(typeof context[name] === "function" && !context[name].iterator) {
194 return context[name].apply(context);
196 if(context[name] !== undefined) {
197 return context[name];
199 // silently ignore unkown variables
206 Does away with nasty characters
208 escape: function(s) {
209 return ((s == null) ? "" : s).toString().replace(/[&"<>\\]/g, function(s) {
211 case "&": return "&";
212 case "\\": return "\\\\";;
213 case '"': return '\"';;
214 case "<": return "<";
215 case ">": return ">";
222 Merges all properties of object `b` into object `a`.
223 `b.property` overwrites a.property`
225 merge: function(a, b) {
228 if(a.hasOwnProperty(name)) {
229 _new[name] = a[name];
233 if(b.hasOwnProperty(name)) {
234 _new[name] = b[name];
240 // by @langalex, support for arrays of strings
241 create_context: function(_context) {
242 if(this.is_object(_context)) {
244 } else if(this.pragmas["IMPLICIT-ITERATOR"]) {
245 var iterator = this.pragmas["IMPLICIT-ITERATOR"].iterator || ".";
247 ctx[iterator] = _context
252 is_object: function(a) {
253 return a && typeof a == "object";
256 is_array: function(a) {
257 return Object.prototype.toString.call(a) === '[object Array]';
260 is_iterator : function(f) {
261 return (typeof f === 'function' && f.iterator);
265 Gets rid of leading and trailing whitespace
268 return s.replace(/^\s*|\s*$/g, "");
272 Why, why, why? Because IE. Cry, cry cry.
274 map: function(array, fn) {
275 if (typeof array.map == "function") {
279 var l = array.length;
281 r.push(fn(array[i]));
290 version: "0.2.3-dev",
293 Turns a template and view into HTML
295 to_html: function(template, view, partials, send_fun) {
296 var renderer = new Renderer();
298 renderer.send = send_fun;
300 renderer.render(template, view, partials);
302 return renderer.buffer.join("\n");
305 escape : function(string) {
306 var renderer = new Renderer();
307 return renderer.escape(string);
313 exports.to_html = Mustache.to_html;
314 exports.escape = Mustache.escape;