You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+86-2Lines changed: 86 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -33,6 +33,48 @@ The template object is based on an actual HTML `<template>` element and created
33
33
34
34
When a template is rendered it is cloned along with the part metadata. Values are set via `setAttribute()` and `textContent`. Some state is stored on the container to indicate that a template was already rendered there. Subsequent renders use that state to update only the parts, not the entire template instance.
35
35
36
+
### Rough Algorithm
37
+
38
+
#### `html`:
39
+
40
+
1.`html` is invoked with `strings` and `values`.
41
+
42
+
`strings` are the literal parts as a `TemplateStringsArray`. The same instance is returned for every evaluation of a template literal.
43
+
44
+
2. Look up a cached template keyed by `strings`, otherwise
45
+
46
+
3. Create a new template:
47
+
48
+
1. Join `strings` with a special marker, `{{}}`. This creates a template string that looks like a Polymer template with empty expressions.
49
+
50
+
2. Create a new `<template>` element and set it's `innerHTML` to the tempalte string.
51
+
52
+
3. Crawl the `<template>` looking for expressions and remember their location by index in `Part` objects. Text-content expressions create new empty text nodes which are used as placeholders.
53
+
54
+
6. Return a `TemplateResult` with the template and the values.
55
+
56
+
#### `TemplateResult.renderTo(container)`:
57
+
58
+
1. Look for an existing `TemplateInstance` on `container`
59
+
60
+
2. If an instance exists, check that it's from the same `Template` as this `TemplateResult`
61
+
62
+
3. If the instance from the same `Template`, remove it's content from `container`
63
+
64
+
4. If an instance doesn't exist, create on from the `Template`:
65
+
66
+
1. Clone the `Template`'s `<template>` contents.
67
+
68
+
2. Iterate through the cloned nodes and create new "instance parts" for nodes that have `Part`s. An "instance part" is just a {part, node} record.
69
+
70
+
5. Update. For every `Part`:
71
+
72
+
1. If it's an attribute part, build up an attibute value then set the attribute.
73
+
74
+
2. If it's a node part, get the value (trampoline thunks, render nested templates, etc), then either append a document fragment or set the `textContent`.
75
+
76
+
6. If this is the first render, append the result DOM to `container`.
77
+
36
78
## Use in Components
37
79
38
80
HTML template coule easily be the basis for component rendering, similar to JSX in React. A component base class can call an instance method that returns a `TemplateResult` and then apply it to the shadow root:
@@ -48,12 +90,54 @@ class MyElement extends CoolBaseElement {
48
90
body =`It's got potential.`;
49
91
50
92
render() { returnhtml`
51
-
<h1>${title}</h1>
52
-
<p>${body}</p>
93
+
<h1>${this.title}</h1>
94
+
<p>${this.body}</p>
53
95
`;}
54
96
}
55
97
```
56
98
99
+
## Benefits over HTML templates:
100
+
101
+
`lit-html` has basically all of the benefits of HTML-in-JS systems like JSX.
102
+
103
+
### Lighter weight
104
+
105
+
There's no need to load expression parser and evaluator.
106
+
107
+
### Seamless access to data
108
+
109
+
Since template literals are evaluated in JavaScript, their expressions have access to every variable in that scope, including globals, module and block scopes, and `this` inside methods.
110
+
111
+
If the main use of templates is to inject values into HTML, this breaks down a major barrier between templates and values.
112
+
113
+
### Faster expression evaluation
114
+
115
+
They're just JavaScript expressions
116
+
117
+
### IDE support by default
118
+
119
+
In a type-checking environment like TypeScript, expressions are checked because they are just regular script. Hover-over docs and code-completion just work as well.
120
+
121
+
## Benefits over JSX
122
+
123
+
### Native syntax
124
+
125
+
No tooling required. Understood by all JS editors and tools.
126
+
127
+
### No VDOM overhead
128
+
129
+
`lit-html` only re-renders the dynamic parts of a template, so it doesn't produce a VDOM tree of the entire template contents, it just produces new values for each expression.
130
+
131
+
### Scoped
132
+
133
+
JSX requires that the compiler be configured with the function to compile tags to. You can't mix two different JSX configurations in the same file.
134
+
135
+
The `html` template tag is just a variable, probably set to a imported function. You can have any number of similar functions in the same JS scope, or set `html` to different implementations.
136
+
137
+
### Template are values
138
+
139
+
JSX translates to function calls, can can't be manipulated on a per-template basis at runtime. `lit-html` produces a template object at runtime, which can be further processed by libraries like ShadyCSS.
0 commit comments