# lit-html
**Repository Path**: mirrors_github/lit-html
## Basic Information
- **Project Name**: lit-html
- **Description**: HTML template literals in JavaScript
- **Primary Language**: Unknown
- **License**: BSD-3-Clause
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2020-08-08
- **Last Updated**: 2026-02-21
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# lit-html
HTML templates, via JavaScript template literals
[](https://travis-ci.org/Polymer/lit-html)
## Overview
`lit-html` lets you write [HTML templates](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template) with JavaScript [template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals), and efficiently render and _re-render_ those templates to DOM.
```javascript
import {html, render} from 'lit-html';
// This is a lit-html template function. It returns a lit-html template.
const helloTemplate = (name) => html`
Hello ${name}!
`;
// Call the function with some data, and pass the result to render()
// This renders
Hello Steve!
to the document body
render(helloTemplate('Steve'), document.body);
// This updates to
Hello Kevin!
, but only updates the ${name} part
render(helloTemplate('Kevin'), document.body);
```
`lit-html` provides two main exports:
* `html`: A JavaScript [template tag](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_template_literals) used to produce a `TemplateResult`, which is a container for a template, and the values that should populate the template.
* `render()`: A function that renders a `TemplateResult` to a DOM container, such as an element or shadow root.
### Announcement at Polymer Summit 2017
## Motivation
`lit-html` has four main goals:
1. Efficient updates of previously rendered DOM.
2. Expressiveness and easy access to the JavaScript state that needs to be injected into DOM.
3. Standard JavaScript without required build steps, understandable by standards-compliant tools.
4. Very small size.
## How it Works
`lit-html` utilizes some unique properties of HTML `` elements and JavaScript template literals. So it's helpful to understand them first.
### Tagged Template Literals
A JavaScript template literal is a string literal that can have other JavaScript expressions embedded in it:
```javascript
`My name is ${name}.`
```
A _tagged_ template literal is prefixed with a special template tag function:
```javascript
let name = 'Monica';
tag`My name is ${name}.`
```
Tags are functions of the form: `tag(strings, ...values)`, where `strings` is an immutable array of the literal parts, and values are the results of the embedded expressions.
In the preceding example, `strings` would be `['My name is ', '.']`, and `values` would be `['Monica']`.
### HTML `` Elements
A `` element is an inert tree of DOM (script don't run, images don't load, custom elements aren't upgraded, etc) that can be efficiently cloned. It's usually used to tell the HTML parser that a section of the document must not be instantiated when parsed, but by code at a later time, but it can also be created imperatively with `createElement` and `innerHTML`.
### Template Creation
The first time `html` is called on a particular template literal it does one-time setup work to create the template. It joins all the string parts with a special placeholder, `"{{}}"`, then creates a `` and sets its `innerHTML` to the result. Then it walks the template's DOM and extracts the placeholder and remembers their location.
Every call to `html` returns a `TemplateResult` which contains the template created on the first call, and the expression values for the current call.
### Template Rendering
`render()` takes a `TemplateResult` and renders it to a DOM container. On the initial render it clones the template, then walks it using the remembered placeholder positions, to create `Part`s.
A `Part` is a "hole" in the DOM where values can be injected. `lit-html` includes two type of parts by default: `NodePart` and `AttributePart`, which let you set text content and attribute values respectively. The `Part`s, container, and template they were created from are grouped together in an object called a `TemplateInstance`.
Rendering can be customized by providing alternate `render()` implementations which create different kinds of `TemplateInstances` and `Part`s, like `PropertyPart` and `EventPart` included in `lib/lit-extended.ts` which let templates set properties and event handlers on elements.
## Performance
`lit-html` is designed to be lightweight and fast (though performance benchmarking is just starting).
* It utilizes the built-in JS and HTML parsers - it doesn't include any expression or markup parser of its own.
* It only updates the dynamic parts of templates - static parts are untouched, not even walked for diffing, after the initial render.
* It uses cloning for initial render.
This should make the approach generally fast and small. Actual science and optimization and still TODOs at this time.
## Features
### Simple expressions and literals
Anything coercible to strings are supported:
```javascript
const render = () => html`foo is ${foo}`;
```
### Attribute-value Expressions
```javascript
const render = () => html``;
```
### SVG Support
To create partial SVG templates - template that will rendering inside and `