Last active
April 26, 2023 08:27
-
-
Save fproperzi/ed449497a1f7e1c89dca3fd414adb3f9 to your computer and use it in GitHub Desktop.
This file contains 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
//https://pablotron.org/2022/03/09/fastest-js-html-escape/ | |
const _esc = (v) => v | |
.replaceAll('&', '&') | |
.replaceAll('<', '<') | |
.replaceAll('>', '>') | |
.replaceAll("'", ''') | |
.replaceAll('"', '"'); | |
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