Skip to content

Instantly share code, notes, and snippets.

@JosePedroDias
Last active August 29, 2015 14:03
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 JosePedroDias/e613cec505a54d78b94f to your computer and use it in GitHub Desktop.
Save JosePedroDias/e613cec505a54d78b94f to your computer and use it in GitHub Desktop.
dumb tpl expansions
var fetchFnComment = function(f) {
return f.toString()
.replace(/^[^\/]+\/\*!?/, '')
.replace(/\*\/[^\/]+$/, '');
};
var applyTemplate = function(targetEl, tpl, model) {
tpl = fetchFnComment(tpl);
for (var k in model) {
tpl = tpl.replace( new RegExp('({{'+k+'}})', 'g'), model[k] );
}
targetEl.innerHTML = tpl;
};
var tpl1 = function() {/*
<b>Hello {{name}}!</b><br/>
My name is {{name}} and I'm {{age}}.
*/};
applyTemplate(ctn, tpl1, {name:'Johnny', age:20});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment