Skip to content

Instantly share code, notes, and snippets.

@skopp
Last active December 18, 2015 23:39
Show Gist options
  • Save skopp/5863470 to your computer and use it in GitHub Desktop.
Save skopp/5863470 to your computer and use it in GitHub Desktop.
whiskers js

whiskers.js

// whiskers.js templating library v0.2.5
(function(a) {
function b(a, b) {
var c = b.split(".");
for (var d = 0; d < c.length; d++) {
if (!a) return "";
a = a[c[d]]
}
return a || ""
}
a.cache = {}, a.render = function(b, c) {
return a.cache[b] || (a.cache[b] = a.compile(b)), a.cache[b](c)
}, a.compile = function(c) {
if (c instanceof Function) return c;
c = (c || "") + "", c = c.replace(/\\/g, "\\\\").replace(/\'/g, "\\'").replace(/\n/g, "\\n").replace(/\r/g, ""), c = c.replace(/(\\*){![\s\S]*?!}/g, function(a, b) {
return b ? a.replace("\\\\", "") : ""
});
var d = [],
e;
c = c.replace(/(\\*){(?:([\w_.]+)|>([\w_.]+)|for +([\w_]+) +in +([\w_.]+)|if +(not +|)([\w_.]+)|\/(for|if))}/g, function(a, b, c, f, g, h, i, j, k, l, m) {
if (b) return a.replace("\\\\", "");
if (c) {
if (c == "else") {
e = d[d.length - 1];
if (e && !e.elsed) {
e.elsed = !0;
if (e.statement == "if") return "'}else{b+='";
if (e.statement == "for") return "'}if(!g(c,'" + e.key + "')){b+='"
}
return console.warn("extra {else} ignored"), ""
}
return "'+g(c,'" + c + "')+'"
}
return f ? "'+r(g(c,'" + f + "'),c)+'" : h ? (d.push({
statement: "for",
key: h
}), "';var " + g + "A=g(c,'" + h + "');for(var " + g + "I=0;" + g + "I<" + g + "A.length;" + g + "I++){c['" + g + "']=" + g + "A[" + g + "I];b+='") : j ? (d.push({
statement: "if"
}), "';if(" + (i ? "!" : "") + "g(c,'" + j + "')){b+='") : k ? (e = d[d.length - 1], e && e.statement == k ? (d.pop(), "'}b+='") : (console.warn("extra {/" + k + "} ignored"), "")) : a
});
for (var f = d.length - 1; f > -1; f--) e = d[f], console.warn("extra {" + e.statement + "} closed at end of template"), c += "'}b+='";
var g = new Function("g", "r", "return function(c){var b='" + c + "';return b}");
return g(b, a.render)
}, a.__express = function() {
try {
return require("./__express")
} catch (a) {}
}()
})(typeof module == "object" ? module.exports : window.whiskers = {})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment