js/template.js

18 lines
434 B
JavaScript
Raw Normal View History

/*
A simple function to more easily write HTML in Javascript
Example:
const greeting = name => template`<p>Hello, ${name}!</p><br>`
document.append(greeting(bob).cloneNode(true))
*/
2021-02-17 10:02:58 +00:00
export const template = (strings, ...args) => {
let buf = []
2021-05-20 11:15:05 +00:00
for (let i=0;i<strings.length;i++) {
buf.push(strings[i], args[i])
}
let template = document.createElement("template")
template.innerHTML = buf.join("")
2021-02-22 19:24:17 +00:00
return template.content
}