Skip to content

Instantly share code, notes, and snippets.

@holloway
Created April 22, 2015 08:44
Show Gist options
  • Save holloway/7c2a5aa7aa38122983db to your computer and use it in GitHub Desktop.
Save holloway/7c2a5aa7aa38122983db to your computer and use it in GitHub Desktop.
/* An HTML template in 180 bytes (minified)
Features: Escapes HTML, accepts either strings or functions as values, and that's all (it doesn't handle looping).
Usage:
OneEightyT(
"<h1>{name}</h1><div>{content} @ {currentTime}</div>",
{
name: "Stella",
content: "1 <3 U",
currentTime: Date.now
}
);
// makes "<h1>Stella</h1><div>1 &lt;3 U @ 1429683130191</h1>"
*/
// UNMINIFIED
var OneEightyT = function(template, dict) {
var value, div = document.createElement("div");
return template.replace(/{(.*?)}/g, function(a, key) {
value = dict[key];
div.textContent = value;
return typeof value == "function" ? value() : div.innerHTML
})
};
// MINIFIED 180 bytes
var OneEightyT=function(n,e){var t,r=document.createElement("div");return n.replace(/{(.*?)}/g,function(n,c){t=e[c];r.textContent=t;return typeof t=="function"?t():r.innerHTML})};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment