Skip to content

Commit 2b44590

Browse files
committed
More README
1 parent 84a10d2 commit 2b44590

1 file changed

Lines changed: 86 additions & 2 deletions

File tree

README.md

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,48 @@ The template object is based on an actual HTML `<template>` element and created
3333

3434
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.
3535

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+
3678
## Use in Components
3779

3880
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 {
4890
body = `It's got potential.`;
4991

5092
render() { return html`
51-
<h1>${title}</h1>
52-
<p>${body}</p>
93+
<h1>${this.title}</h1>
94+
<p>${this.body}</p>
5395
`;}
5496
}
5597
```
5698

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.
140+
57141
## Features
58142

59143
### Simple expressions and literals

0 commit comments

Comments
 (0)