Add template module to more easily write templates
This commit is contained in:
parent
2c5a6d323e
commit
1e6c0de2ec
1 changed files with 18 additions and 0 deletions
18
template.js
Normal file
18
template.js
Normal 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)
|
||||
}
|
Loading…
Reference in a new issue