Skip to content

Instantly share code, notes, and snippets.

@capitalist
Created January 27, 2011 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save capitalist/799073 to your computer and use it in GitHub Desktop.
Save capitalist/799073 to your computer and use it in GitHub Desktop.
Mustache.to_javascript("Hello {{planet}}")
function (obj) {
var out, l1, stack, fetch, escape, isFunction;
out = [];
stack = [];
stack.push(obj);
fetch = function fetch(key) {
var i, v;
for (i = stack.length - 1; i >= 0; i -= 1) {
v = stack[i][key];
if (v) {
return v;
}
}
};
escape = function escape(value) {
return ('' + value)
.replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;');
};
isFunction = function isFunction(obj) {
return !!(obj && obj.constructor && obj.call && obj.apply);
};
out.push("Hello ");
l1 = fetch("planet");
if (isFunction(l1)) {
l1 = l1();
}
out.push(escape(l1));
return out.join("");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment