Skip to content

Instantly share code, notes, and snippets.

@marcuswestin
Created December 2, 2011 22:28
Show Gist options
  • Save marcuswestin/1425138 to your computer and use it in GitHub Desktop.
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.
<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>
@marcuswestin
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment