In the previous module you wrote several templates with the html function, but always with completely fixed content: the same title, the same status, the same assignee on every render. The time has come to understand what that html function really is and, above all, what happens under the hood when its content stops being static. This lesson opens module 2 by explaining the internal mechanism of Lit's template engine: how a template is analyzed, how the DOM is updated when data changes, and why this approach is much more efficient than the classic alternatives of manipulating the DOM by hand or rebuilding it with innerHTML.

Contents

  1. html is not a text template: it's a function
  2. Tagged template literals: the foundation of html
  3. How Lit knows which parts are dynamic
  4. Updating the DOM without a Virtual DOM
  5. Why this is different (and better) than innerHTML
  6. First interpolation in <task-card>
  7. Important: this still isn't full reactivity

  1. html is not a text template: it's a function

So far, every time you've written something like this:

render() {
  return html`
    <article>
      <h3>Preparar la demo del sprint</h3>
      <p>Estado: En curso</p>
    </article>
  `;
}

it's easy to think of html\...`as if it were simply a text string with HTML inside, something similar to writing"

...

". That intuition is reasonable at first glance, but it's incorrect, and it hides exactly the mechanism that makes Lit fast and practical to use. html` is, in fact, a JavaScript function, and what gets passed between the backticks isn't a normal string, but something called a tagged template literal, a feature that has been part of the JavaScript standard itself for years, with no dependency on Lit whatsoever.

When the JavaScript engine encounters html\

Hola

`, it does not concatenate text and call htmlwith the result. Instead, it invokes thehtmlfunction with structured information about the template: on one hand, an array with the literal ("static") text chunks, and on the other, the values that have been interpolated inside${}` (if any). This distinction —fixed chunks on one side, dynamic values on the other— is the central piece of everything that makes Lit's template engine special.

  1. Tagged template literals: the foundation of html

To properly understand what html does, it's useful to first see how a tagged template literal works with a sample function, without Lit involved:

function miEtiqueta(trozos, ...valores) {
  console.log(trozos);   // ["Hola ", ", tienes ", " tareas"]
  console.log(valores);  // ["Ana", 3]
}

const nombre = 'Ana';
const numeroDeTareas = 3;

miEtiqueta`Hola ${nombre}, tienes ${numeroDeTareas} tareas`;

When this code runs, trozos receives an array with the three text fragments that remain between the interpolations ("Hola ", ", tienes " and " tareas"), while valores receives, in a separate array, the two interpolated values ("Ana" and 3). This separation is exactly what makes it possible for a tagged function to "know" which parts of the template are literal (they never change) and which are expressions (they can change).

Lit's html function is, in essence, a tagged function of this same kind, specialized in treating the first array as HTML and the second as data that must be inserted at specific points within that HTML. The difference from the miEtiqueta example is that html doesn't just log those values to the console: it builds from them an internal object, called a TemplateResult, which describes the template in a way that Lit can process efficiently.

  1. How Lit knows which parts are dynamic

Here lies the key to the whole mechanism. Since the literal text chunks always arrive separately from the interpolated values, Lit can, the first time it sees a particular template, analyze only its fixed "skeleton" (the text chunks) and figure out exactly at which positions of the HTML dynamic values will appear. That analysis is done only once per template, not on every render.

The process, simplified, is as follows:

  1. The first time Lit encounters a given html\...`template (identified internally by its fixed text chunks), it builds a real HTML