Skip to content

Instantly share code, notes, and snippets.

@fproperzi
Last active April 26, 2023 08:27
Show Gist options
  • Save fproperzi/ed449497a1f7e1c89dca3fd414adb3f9 to your computer and use it in GitHub Desktop.
Save fproperzi/ed449497a1f7e1c89dca3fd414adb3f9 to your computer and use it in GitHub Desktop.
//https://pablotron.org/2022/03/09/fastest-js-html-escape/
const _esc = (v) => v
.replaceAll('&', '&')
.replaceAll('<', '&lt;')
.replaceAll('>', '&gt;')
.replaceAll("'", '&apos;')
.replaceAll('"', '&quot;');
var a = {a:'vispa',b:'teresa', list:[{v:'1',t:'uno'},{v:'2',t:'due'},{v:'3',t:'t<r>e'}]}
const fn = (j) => `
la ${j.a} ${j.b} contava
<ul>
${j.list.map(
(k)=>`<li value="${_esc(k.v)}">${_esc(k.t)}</li>`
).join('')}
</ul>`
//--------------------------
var data = {
title: 'Some users...',
rows: [
{name: 'Jaap', age: 32, gender: 'M'},
{name: 'Marieke', age: 27, gender: 'F'},
{name: 'Sanne', age: 17, gender: 'F'}
]
};
const tmpl = (d)=>`
<h2>${d.title}</h2>
<table border="1">
${d.rows.map(
(r) => `<tr><th><${_esc(r.name)}/th><td>${r.age}</td><td>${_esc(r.gender)}</td></tr>`
).join('')}
</table>
`;
//-----------------------------------------------
/* Nano Templates - https://github.com/trix/nano */
function nano(template, data) {
return template.replace(/\{([\w\.]*)\}/g, function(str, key) {
var keys = key.split("."), v = data[keys.shift()];
for (var i = 0, l = keys.length; i < l; i++) v = v[keys[i]];
return (typeof v !== "undefined" && v !== null) ? v : "";
});
}
var listTemplate = [
'<li class="ui-first-child ui-last-child">',
'<a href="#page-card" data-id="{id}" class="ui-btn ui-btn-icon-right ui-icon-carat-r">',
'<h2>{name}</h2>',
'<p><strong>{address.city}</strong></p>',
'<p>{email}</p>',
'<p class="ui-li-aside">id: <strong>{id}</strong></p>',
'</a>',
'</li>'
].join("");
nano(listTemplate, item)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment