Whats next in templating
Filip Bech-Larsen
@IMPACTdigitaldk
@filipbech
#weAreHiring #Aarhus #Cph
https://developers.google.com/experts/people/filip-bruun-bech-larsen
Whats next in templating
Filip Bech-Larsen
@IMPACTdigitaldk
understand details
to gain perspective
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…
Not entirely my idea…
• hyperHTML
• lit-html
• innerself
understand details
to gain perspective
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
• tiny… <1kb
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)
• 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
lit-html-element, @polymer/litElement, more…
understand details
to gain perspective
• superpowers properties
• change => invalidation
• types (for conversion), attributes reflection, default value
• computed properties
• batching rendering
• lifecycle hooks
• easy id element-queries
lit-html-element
https://github.com/kenchris/lit-element
… with decorators
https://github.com/tc39/proposal-decorators
https://material-components.github.io/material-components-web-components/demos/index.html
one more thing…
element-router
• native web-component router (no dependencies)
• syntax/api almost like Preact-router
• <1 kb
• works with lit-html, hyperHTML, or nothing or anything else really…
https://github.com/filipbech/element-router
templating with lit
is pretty sweet
closing thoughts…
Thank you!
This was “Whats next in templating”
I’m @filipbech
@IMPACTdigitaldk
Follow for slides

Whats next in templating