juliocastillo.dev YaDS - Yet another Developer's Site

lit-html templates from zero to hero


📣 UPDATE! 📣 Now lit-html and LitElement are unified under Lit. I'm writing new posts about Lit, meanwhile you can:

  • read this post because the principles are the same
  • upgrade your code with this [guide] (https://lit.dev/docs/releases/upgrade/)
  • visit Lit site to know what's new

You can also use lit-html standalone

After a very smooth introduction to lit-html, I will go hard and get my hands dirty. I will show you how to write templates so this will be a session full of code!

I know you are here because you want to learn LitElement, so you read this title and find out that we continue with lit-html. The reason why I've structured these sessions this way is because lit-html is the basis for LitElement and also because I want you to see the great potential and versatility of lit-html by itself, probably the perfect match for a small or medium project. I assure you that everything you learn about lit-html will be used in LitElement. Be patient, we will arrive soon.

Writing templates

☝️Remember from last post: templates are written using tagged template literals. We tag the template literal with the html function provided by lit-html. This function returns a TemplateResult object. render is the other function provided by lit-html. render receives a TemplateResult object and renders it into the DOM.

This is a very simple example:

// import functions from lit-html library
import {html, render} from 'lit-html';

// define the template as function that takes the data
// and returns a TemplateResult object
const template = name => {
return html`

// node where the template will be rendered
const mainNode = document.getElementById('main');

// render the template
render(template('Silvia'), mainNode);

// render the template again with different data
render(template('Benjamin'), mainNode);

The dynamic parts of a template are JavaScript expressions that are binded with values. TemplateResult supports bindings of certain data types that we will see now.

Supported bindings

// Text binding
html`<div>${capitalize(user.name, user.firstName)}</div>`;

/**** Given theses variables and values... **************

let someText = 'Lorem ipsum';
let user = { name : 'JEN', firstName: 'MONROE' };

***** The code above will render... *********************

<p>Lore ipsum</p>
<div>Jen Monroe</div>

// Attribute binding
html`<div class=${classList}>Stylish text.</div>`;

/**** Given these variables and values... *****************

let classList = ['main', 'required', 'modern'].join(' ');

***** The code above will render... ***********************

<div class="main required modern">Stylish text.</div>

// Attribute binding (boolean)
html`<input type="submit" ?disabled=${formWithErrors} value="Submit">
<span ?hidden=
${!formWithErrors}>Form has errors!</span>`

/**** Given these variables and values... *****************

let formWithErrors = true;

***** The code above will render... ***********************

<input type="submit" disabled value="Submit">`;
<span>Form has errors!</span>`

// Property binding
html`<custom-list .items=${users} id="user-list"></custom-list>`;

/**** Given these variables and values... *****************

const users = ['Diego', 'Ana', 'Laura', 'Piero'];
const customList = document.getElementById('user-list');

***** The code above will show in console *****************

["Diego", "Ana", "Laura", "Piero"]


// Event binding to a global handler function
html`<button @click=${handleClick}>Click Me!</button>`;

// Event binding to an inline function
html`<button @click=${()=>console.log('clicked')}>Click Me!</button>`;

// Event binding to an instance function
html`<button @click=${this.handleClick}>Click Me!</button>`;

// Event binding to listener object
html`<button @click=${clickHandler}>Click Me!</button>`;

const clickHandler = {
handleEvent(e) { console.log('clicked!'); }

// Binding to a DOM node
const h1 = document.createElement('h1');
h1.textContent = 'Chapter 1'
const page = html`${h1}<p>Once upon a time...</p>`;

/***** The code above will render... **********************

<h1>Chapter 1</h1>
<p>Once upon a time...</p>

// Binding to a TemplateResult object
const header = html`<h1>Chapter 1</h1>`;
const article = html`<article>${header}<p>Once upon a time...</p></article>`;

/***** The code above will render... **********************

<h1>Chapter 1</h1>
<p>Once upon a time...</p>

// Binding to arrays/iterables
const items = [1, 2, 3];
const listItems = items.map(i => html`<li>${2*i}</li>`);
const template = html`<ul>${listItems}</ul>`;

/***** The code above will render... **********************


// Binding to a promise
const text = fetch(url).then(response => response.text());
const page = () => html`<p>${text}</p>`;

Let's say that after some seconds the fetch operation
resolves with a the string 'some text...'

Until the promise is resolved, the code above will render

Once the promise is resolved, it will render...
<p>some text...</p>



One consequence of having bindings to expressions that return TemplateResult objects is that by composition we can create templates using other templates. The composition allows:

const header = data => html`
${data.subtitle ? html`<h2>${data.subtitle}<h2>` : ''}`

const main = data => html`<p>${makeParagraph(data.text)}</p>`;

const composedTemplate = data => html`


A template can have parts that are only visible if a condition is met, or it could have parts that are represented in different ways depending on one or more conditions. These behaviors can be expressed using conditional expressions such as the ternary operator ? or conditional structures such as the if or switch-case statements.

// using ternary operator
const main = data => html`
${data.text ?
html`<p>${data.text}</p>` :
html`<img src=${data.image}></img>`}

// using if
const main = data => {
if (data.text) {
return html`<p>${data.text}</p>` :
} else {
return html`<img src=${data.image}></img>`;

// using switch-case
const main = data => {
switch (data.screenSize) {
case 's':
return html`<img src="${data.image}-small.png"></img>`;
case 'm':
return html`<img src="${data.image}-medium.png"></img>`;
case 'l':
return html`<img src="${data.image}-medium.png"></img>
<img src="
return html`<p>${data.text}</p>`;


It is very common for a part of a template to be repeated with different values. Think of a list, where the part that paints each item is always the same but with different values. For these cases we have already seen that it is possible to make a binding to expressions that return iterables from TemplateResult objects. This is the way we can repeat parts or blocks in a template.

const weekDays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Staurday', 'Sunday'];

// loops with arrays
html`<ul>${weekDays.map((day) => html`<li>${day}</li>`)}</ul>`;

// loop statement (better if DOM node update is cheap)
const itemTemplates = [];
for (const day of weekDays) {

* We'll see the use of directives
* in a future post !!

// repeat directive (better if moving DOM node is cheap)
html`<ul>${repeat(weekDays, day => day /*id*/, (day, index) => html`<li>${day}</li>`)}</ul>`;

Setting things up

lit-html is distributed as a npm package so to install it run the command:

npm install --save lit-html

lit-html uses JavaScript modules therefore to use the html and render functions we have to import them from the module.

At the moment, browsers don't import ES6 modules using the short syntax:

import {html, render} from 'lit-html'

We should write the full path to the module

import {html, render} from './node_modules/lit-html/lit-html.js'

But if you want to use the short syntax you can use a built tool such as Webpack or you can install the Polymer command line tool that can handle the short syntax import when it serves the application.

With Polymer CLI:

## install it
npm install -g polymer-cli

## use it
polymer serve

You're ready to start coding! I strongly recommend you to experiment and try your own examples. The best way to learn is by doing!


As we have seen, a template is pure JavaScript code so any expression and valid structure of the language can be used inside the template. There are so many ways to define a template that is only limited by the preferences or styles of each developer. The result is that the templates turn out to be tremendously expressive and because it is just JavaScript you don't need to learn nothing new.

comments powered by Disqus