Created
December 2, 2011 22:28
-
-
Save marcuswestin/1425138 to your computer and use it in GitHub Desktop.
Who needs fancy javascript templating languages when a good old 10-liner does the job.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="output"></div> | |
<script type="text/html" id="greeting-template"> | |
<div class="greeting"> | |
Hello, {{ name }}! | |
</div> | |
</script> | |
<script> | |
function template(id, params) { | |
var html = document.getElementById(id).innerHTML | |
for (var key in params) { | |
var search = '{{ '+key+' }}' | |
while (html.match(search)) | |
html = html.replace(search, params[key]) | |
} | |
return html | |
} | |
var el = document.getElementById('output') | |
el.innerHTML = template('greeting-template', { name:'Templated World' }) | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In action: http://jsfiddle.net/DvJyV