Skip to content

Instantly share code, notes, and snippets.

@dciccale
Created June 4, 2014 11:10
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 dciccale/bbdb4a816775a6b186a0 to your computer and use it in GitHub Desktop.
Save dciccale/bbdb4a816775a6b186a0 to your computer and use it in GitHub Desktop.
Client-side template engine
<!doctype html>
<script>function t(s,d){for(var k in d)s=s.replace(new RegExp('{{\\s*'+k+'\\s*}}','g'),d[k]);return s;}</script>
<script type="template" id="tmpl">
<h1>Hello {{ name }}!</h1>
</script>
<script>
var tmpl = document.getElementById('tmpl').innerHTML;
var data = {name: 'denis'};
document.body.innerHTML = t(tmpl, data);
</script>
function t(s,d){for(var k in d)s=s.replace(new RegExp('{{\\s*'+k+'\\s*}}','g'),d[k]);return s;}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment