-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.js
More file actions
30 lines (29 loc) · 830 Bytes
/
template.js
File metadata and controls
30 lines (29 loc) · 830 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import tint from './index.js'
export default (element, template, doc) => {
doc = doc || document
const compile = tint((tagName, attributes, children) => {
const element = doc.createElement(tagName)
Object.keys(attributes).forEach(key => {
const v = attributes[key]
if (typeof v == 'function') {
element.addEventListener(
key.substr(key.substr(0, 2) == 'on' ? 2 : 0), v
)
} else if (v != null && v !== false) {
element.setAttribute(key, v === true ? '' : v)
}
})
children.forEach(child => {
element.appendChild(child)
})
return element
}, str => doc.createTextNode(str), doc)
const render = compile(element, template)
var e = element
return scope => {
const x = render(scope)
e.replaceWith(x)
e = x
return e
}
}