Add template module to more easily write templates

This commit is contained in:
Talia 2020-10-27 16:29:29 +01:00
parent 2c5a6d323e
commit 1e6c0de2ec

18
template.js Normal file
View file

@ -0,0 +1,18 @@
function setup(root) {
root.parts = {}
root.querySelectorAll("[part-id]").forEach( element => {
root.parts[element.getAttribute("part-id")] = element
})
root.clone = function() { return setup(this.cloneNode(true)) }
return root
}
function template(strings, ...args) {
let buf = []
for (i=0;i<strings.length;i++) {
buf.push(strings[i], args[i])
}
let template = document.createElement("template")
template.innerHTML = buf.join("")
return setup(template.content)
}