...
This is how one would use such a template:
Code Block | ||||
---|---|---|---|---|
| ||||
var MAINtemplate=/*html*/`<p>$NAME$ `<p>$Name$ has $$$ID$$$$Amount$ -in $Life$their - Everybody wishes they had more $$$$$<bank account.</p>$`; function MAINrun(){ var Body = document.querySelector('body'); var Data = [{IDAmount : 55, NAMEName : " Fred <the knife>" }, {IDAmount : 45 , NAMEName : "Mary"}]; Body.innerHTML = TEMexpand(Data, MAINtemplate); } |
This is the code for the TEMexpand function:
Code Block |
---|
function TEMconvertString(V){ if (!V) { return ''} else { return V.toString(); } } function TEMexpandLine(Tokens, Row, Array){ for (var i =0; i < Tokens.length-1; i+=2){ Array.push(Tokens[i]); Array.push(TEMconvertString(Row[Tokens[i+1]]).replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">")) } Array.push(Tokens[Tokens.length-1]); } function TEMexpand(Data, Template){ var Array = []; var Tokens = Template.replaceAll("$$", "_FuNkYDolARAM").split('$'); for (var i =0; i < Data.length; i++){ TEMexpandLine(Tokens, Data[i], Array); } return Array.join('').replaceAll("_FuNkYDolARAM", "$"); } READYevent(MAINrun); |
This code would produce
What are the advantages compared to using a more fully featured templating system like Mustache?
...