mustache.js revision 2d7019e19039fd083850f76e54b6b26a7b06a8f2
/*
mustache.js — Logic-less templates in JavaScript
See http://mustache.github.com/ for more info.
*/
var Mustache = function() {
var regexCache = {};
var Renderer = function() {};
otag: "{{",
ctag: "}}",
pragmas: {},
buffer: [],
"IMPLICIT-ITERATOR": true
},
context: {},
// reset buffer & set context
if(!in_recursion) {
this.buffer = []; // TODO: make this non-lazy
}
// fail fast
if(in_recursion) {
return template;
} else {
return;
}
}
// get the pragmas together
// render the template
// render_section did not find any sections, we still need to render the tags
if (html === false) {
}
if (in_recursion) {
return html;
} else {
}
},
/*
Sends parsed lines
*/
if(line !== "") {
}
},
if (text) {
}
}
},
/*
Looks for %PRAGMAS
*/
render_pragmas: function(template) {
// no pragmas
return template;
}
var that = this;
});
throw({message:
"This implementation of mustache doesn't understand the '" +
pragma + "' pragma"});
}
if(options) {
}
return "";
// ignore unknown pragmas silently
});
},
/*
Tries to find a partial in the curent scope and render it
*/
}
}
},
/*
Renders inverted (^) and normal (#) sections
*/
// did not render anything, there were no sections
return false;
}
var that = this;
// This regex matches _the first_ section ({{#foo}}{{/foo}}), and captures the remainder
return new RegExp(
"^([\\s\\S]*?)" + // all the crap at the beginning that is not {{*}} ($1)
otag + // {{
"(\\^|\\#)\\s*(.+)\\s*" + // #foo (# == $2, foo == $3)
ctag + // }}
"\n*([\\s\\S]*?)" + // between the tag ($2). leading newlines are dropped
otag + // {{
"\\/\\s*\\3\\s*" + // /foo (backreference to the opening tag).
ctag + // }}
"\\s*([\\s\\S]*)$", // everything else in the string ($4). leading whitespace is dropped.
"g");
});
// for each {{#foo}}{{/foo}} section do...
// before contains only tags, no sections
// after may contain both sections and tags, so use full rendering function
// will be computed below
// false or empty list, render it
} else {
renderedContent = "";
}
}).join("");
partials, true);
} else if (typeof value === "function") {
// higher order section
});
} else if (value) { // boolean section
} else {
renderedContent = "";
}
}
});
},
/*
Replace {{foo}} and friends with values from our view
*/
// tit for tat
var that = this;
var new_regex = function() {
});
};
switch(operator) {
case "!": // ignore comments
return "";
case "=": // set new delimiters, rebuild the replace regexp
return "";
case ">": // render partial
case "{": // the triple mustache is unescaped
default: // escape the value
}
};
if(!in_recursion) {
}
}
if(in_recursion) {
}
},
set_delimiters: function(delimiters) {
},
escape_regex: function(text) {
// thank you Simon Willison
var specials = [
'/', '.', '*', '+', '?', '|',
'(', ')', '[', ']', '{', '}', '\\'
];
);
}
},
/*
find `name` in current `context`. That is find me a value
from the view object
*/
// Checks whether a value is thruthy or false or 0
function is_kinda_truthy(bool) {
}
var value;
}
if(typeof value === "function") {
}
return value;
}
// silently ignore unkown variables
return "";
},
// Utility methods
/* includes tag */
},
/*
Does away with nasty characters
*/
escape: function(s) {
s = String(s === null ? "" : s);
return s.replace(/&(?!\w+;)|["'<>\\]/g, function(s) {
switch(s) {
case "'": return ''';
default: return s;
}
});
},
// by @langalex, support for arrays of strings
create_context: function(_context) {
if(this.is_object(_context)) {
return _context;
} else {
var iterator = ".";
}
var ctx = {};
ctx[iterator] = _context;
return ctx;
}
},
is_object: function(a) {
},
is_array: function(a) {
return Object.prototype.toString.call(a) === '[object Array]';
},
/*
Gets rid of leading and trailing whitespace
*/
trim: function(s) {
return s.replace(/^\s*|\s*$/g, "");
},
/*
Why, why, why? Because IE. Cry, cry cry.
*/
map: function(array, fn) {
if (typeof array.map == "function") {
return array.map(fn);
} else {
var r = [];
var l = array.length;
for(var i = 0; i < l; i++) {
r.push(fn(array[i]));
}
return r;
}
},
getCachedRegex: function(name, generator) {
var byOtag = regexCache[this.otag];
if (!byOtag) {
byOtag = regexCache[this.otag] = {};
}
var byCtag = byOtag[this.ctag];
if (!byCtag) {
byCtag = byOtag[this.ctag] = {};
}
var regex = byCtag[name];
if (!regex) {
regex = byCtag[name] = generator(this.otag, this.ctag);
}
return regex;
}
};
return({
/*
Turns a template and view into HTML
*/
to_html: function(template, view, partials, send_fun) {
var renderer = new Renderer();
if(send_fun) {
renderer.send = send_fun;
}
renderer.render(template, view || {}, partials);
if(!send_fun) {
return renderer.buffer.join("\n");
}
}
});
}();