WHAT’SNEXTIN
CLIENTSIDETEMPLATING?
by Filip Bech-Larsen
@filipbech
!
#alwaysHiring #Copenhagen
WHAT’SNEXTIN
CLIENTSIDETEMPLATING?
by Filip Bech-Larsen
UNDERSTANDDETAILS
TOGAINPERSPECTIVE
TEMPLATING…
• Building user interface using a declarative format (kinda looks
like html)
• Some parts are static - some are dynamic
UI=F(STATE)
ATRIPDOWNMEMORYLANE…
WHATSNEXT?
LETSTAKEASTEPBACK…
THEJOBOFATEMPLATINGSYSTEM
• Balance between
• developer experience
• fast boot
• fast update
WHATDOESTHEPLATFORMPROVIDE?
• template element
• (tagged) template literals
THETEMPLATEELEMENT
• holds “inert” DOM
• scripts don’t run
• styles don’t apply
• easily cloned
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template
TEMPLATELITERALS
• 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
TAGGEDTEMPLATELITERALS
• a tag is a function that processes the template literal - both the
literal and the expressive parts. Can return anything
TAGGEDTEMPLATELITERALS
• 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
CANWEUSETHISFORCREATING
ACOOLTEMPLATINGSYSTEM?
THEJOBOFATEMPLATINGSYSTEM
• balance between
• developer experience
• fast boot
• fast update
ASOLUTION?
• template in js means tools
• create template to stamp out clone
• identity of staticParts as caching-key
• We know what changed…
NOTENTIRELYMYIDEA…
• hyperHTML
• lit-html
• innerself
UNDERSTANDDETAILS
TOGAINPERSPECTIVE
INTRODUCINGLIT-HTML
LIT-HTML
• A tag-function (called html) that returns templating
instructions (called TemplateResult)
• A render method to produce and maintain dom from
TemplateResult
• Tiny and super fast
https://github.com/Polymer/lit-html
WHATCANLITHANDLE?
TEXTCONTENT
EXPRESSIONS
EXPRESSIONS
ATTRIBUTES
BOOLEANATTRIBUTES
PROPERTIES
EVENT-HANDLERS
TEMPLATERESULT
for composition
ARRAYS
ARRAYS
for composition
DOMNODES
ASYNCVIAPROMISES
streams also supported - either via append or replace strategies
EXTENDINGLIT-HTML
directives
REMEMBERTHIS?
DIRECTIVES
DIRECTIVES
or use await, or simply just setValue(promise)
SOYOUSAYITSFAST?
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)
THATALLGOOD…
how do I make a component?
LITONLYHANDLESTEMPLATES
BRINGYOUROWNCOMPONENT
encapsulation, data-flow, events
IFONLYTHEPLATFORMHADTHAT…
WEB-COMPONENTS
WEBCOMPONENTS
• custom-elements
• shadow-DOM
• template-element
• html-imports
LIT-HTMLANDWEBCOMPONENTS
• 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
DATAFLOWWITHPROPERTIES+EVENTS
THEREAREHELPERSALREADY
@polymer/litElement, lit-html-element, more…
UNDERSTANDDETAILS
TOGAINPERSPECTIVE
• Special typed properties
• React to changes
• Attributes-reflection
• Automatic batching rendering
LITELEMENT
LIVE-CODINGTIME…
wish me luck!
AGOODPLACETOSTART
https://github.com/nicolainormann/lit-element-starter
LIT-HTMLISREALLYSWEET,WITH
LITELEMENTITSALSOVERYUSEFUL
ONEMORETHING…
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
CLOSINGTHOUGHTS…
THANKYOU!
This was “Whats next in templating”
I’m @filipbech
Follow for slides
Whats next in clientside templating

Whats next in clientside templating