Whats next in templating
Filip Bech-Larsen
@IMPACTdigitaldk
@filipbech
#weAreHiring #Aarhus #Cph
https://developers.google.com/experts/people/filip-bruun-bech-larsen
https://www.facebook.com/groups/ngAarhus/
Whats next in templating
Filip Bech-Larsen
@IMPACTdigitaldk
templating…
• Building user interface using a declarative format (kinda looks like html)
• Some parts are static - some are dynamic
UI = f(state)
React really taught this to the world…
a trip down memory lane…
whats next?
lets take a step back…
the job of a templating system
• balance between
• developer experience
• fast boot
• fast update
What does the platform provide?
• template element
• (tagged) template literals
The template element
• holds “inert” DOM
• scripts don’t run
• styles don’t apply
• easily cloned
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template
Template literals
• instead of quotes uses backticks - `
• can span multiple lines
• can embed expressions
• can be tagged…
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
tagged template literals
• a tag is a function that processes the template literal - both the literal and the
expressive parts. Can return anything
tagged template literals
• a tag is a function that processes the template literal - both the literal and the
expressive parts. Can return anything.
• the literal parts-array will be the same (===) in consecutive calls
can we use this for creating a cool
templating system?
the job of a templating system
• balance between
• developer experience
• fast boot
• fast update
a solution?
• template in js means tools
• create template to stamp out clone
• identity of staticParts as caching-key
• We know what changed…
introducing lit-html
lit-html
• A tag-function (called html) that returns templating instructions (called
TemplateResult)
• A render method to produce and maintain dom from TemplateResult
• <2kb gzipped and super fast
https://github.com/Polymer/lit-html
what can lit handle
Text content
expressions
expressions
attributes
TemplateResult
for composition
Arrays
Arrays
for composition
DOM nodes
Async via promises
extending lit-html
directives
remember this?
directives
directives
or use await, or simply just setValue(promise)
lit-extended is sugar
• properties instead of attributes by default
• declarative event-listening syntax
• only 540b gzipped extra…
lit-extended
so you say its fast?
Performance
• 2 aproaches to rendering/updating
• Be smart (Polymer) - scales with complexity (no of changes)
• Be fast (React) - scales with number of nodes (but very fast because of
vdom)
Performance
• 2 aproaches to rendering/updating
• Be smart (Polymer) - scales with complexity (no of changes)
• Be fast (React) - scales with number of nodes (but very fast because of
vdom)
• Lit-html tries to sit in the middle (scales with expressions)
that all good…
how do I make a component?
lit only handles templates
bring your own component
encapsulation, data-flow, events
if only the platform had that…
Web-Components
WebComponents
• custom-elements
• shadow-DOM
• template-element
• html-imports
Lit-html and webcomponents
• Lit is build to be a great fit with web-components
• make a render-method that returns a TemplateResult
• use property setters to invalidate
• re-render when invalidated
• dispatch native DOM-events
there are helpers already
Like LitElement (lit-html-element on npm)
• superpowers properties
• change => invalidation
• types (for conversion), attributes reflection, default value
• computed properties
• batching rendering
• lifecycle hooks
• easy id element-queries
LitElement
https://github.com/kenchris/lit-element
with decorators…
one more thing…
element-router
• native web-component router (no dependencies)
• syntax/api almost like Preact-router
• <1 kb
https://github.com/filipbech/element-router
templating with lit
is pretty sweet
thats it… questions?

Whats next in templating