Skip to content

Instantly share code, notes, and snippets.

@msrafi
Forked from tkissing/LICENSE.txt
Created November 6, 2012 22:49
Show Gist options
  • Save msrafi/4028203 to your computer and use it in GitHub Desktop.
Save msrafi/4028203 to your computer and use it in GitHub Desktop.
mstc - mustache style templating compressed to (less than) 140byt.es

mstc

mstc is a minimal mustache-like template processor. It supports objects, arrays and can even call functions on them - all without using eval or new Function! Placeholders can contain any character except curly braces (that means you can have templates that render into templates).

function(a,b) { // a: template string, b: Object or Array with values to fill in
// coerce a to a string
return(a+'').replace(
// search for anything with a mustache around it
/\{\{([^{}]+)}}/g,
function(c,d) { // c will be the complete placeholder, d the part between the mustaches
// b is optional (but if passed, must be a type that is a valid operand for "in"
return d in (b||{})
// if d is a method of b, call it, otherwise return its value
? (/^f/.test(typeof b[d]) ? b[d]() :b[d] )
// if d is not a member of b, don't replace anything to allow nested calls like
// mstc(mstc(tmpl, obj), anotherObj)
: c;
}
);
}
function(a,b){return(a+'').replace(/\{\{([^{}]+)}}/g,function(c,d){return d in(b||{})?(/^f/.test(typeof b[d])?b[d]():b[d]):c})}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Timo Kissing http://kissing.name
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "mustacheStyleTemplatesCompressed",
"description": "Very minimal implementation of mustache style templating in JavaScript",
"keywords": [
"140bytes",
"mustache",
"template"
]
}
<!DOCTYPE html>
<head>
<title>mstc: mustache style templates - compressed </title>
</head>
<body>
<div>Expected value: <b>Hello @140bytes! How is the weather?</b></div>
<div>Actual value: <b id="ret1"></b></div>
<div>Expected value: <b>The array has a length of 3 and the first element is "foo". Ah, what the heck, here is all of it: foo,bar,bam</b></div>
<div>Actual value: <b id="ret2"></b></div>
<script>
var mstc = function(a,b){return(a+'').replace(/\{\{([^{}]+)}}/g,function(c,d){return d in(b||{})?(/^f/.test(typeof b[d])?b[d]():b[d]):c})};
document.getElementById( "ret1" ).innerHTML = mstc('Hello {{w}}! How is the {{x}}?',{w:'@140bytes', x: function() {return 'weather';}});
document.getElementById( "ret2" ).innerHTML = mstc('The array has a length of {{length}} and the first element is "{{0}}". Ah, what the heck, here is all of it: {{join}}', ['foo', 'bar', 'bam']);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment