/** * I've changed the syntax a little from the blog post. * For full details, see the post itself: * http://sk80.co.uk/2016/08/template-engine-code/ * * util.js: * https://github.com/Skateside/template/blob/master/util.js * template.js: * https://github.com/Skateside/template/blob/master/template.js **/ var string = [ "<ul>", "${#if responses.length}", "${#each responses as response}", "<li>${response.name}</li>", "${#end each}", "${#end if}", "${#if !responses.length}", "<li><em>No responses</em></li>", "${#end if}", "</ul>" ].join("\n"); var data = { responses: [ {name: "Alpha"}, {name: "Bravo"}, {name: "Charlie"} ] }; // Render the template: document.addEventListener("DOMContentLoaded", function () { var myTemplate = template(string); document.body.innerHTML += myTemplate.render(data); });
<p>The template will be render below here:</p>