Sub topic 4
Templates and slots in the platform
This chapter assumes you know how custom elements and shadow roots work from the earlier chapters. It covers two HTML elements, <template> and <slot>. LWC templates and slots are built on these two.
The template element
A <template> element holds markup that the browser parses but does not render. Scripts inside it do not run. Images inside it do not load. The parsed content sits in a DocumentFragment, which you read from the element's content property.
To use the markup, you clone the fragment and insert the clone into the DOM. Cloning is cheaper than parsing a string every time, so a component can create the template once and clone it for each instance.
The slot element
A <slot> is a placeholder inside a shadow tree. The component's user writes child elements between the component's tags. Those children are the light DOM. The browser displays each child at a matching slot position. It does not move the children into the shadow tree. They stay in the light DOM, and the slot only decides where they are drawn.
- A slot with no
nameis the default slot. It receives children that have noslotattribute. - A slot with a
namereceives children whoseslotattribute has the same value. - Content inside a
<slot>is fallback content. It shows only when nothing is assigned to that slot.
A working example
Save this as an HTML file and open it in a browser.
<info-card>
<span slot="title">Release notes</span>
<p>Slotted content stays in the light DOM.</p>
</info-card>
<script>
const template = document.createElement('template');
template.innerHTML = `
<style>.card { border: 1px solid #999; padding: 8px; }</style>
<div class="card">
<header><slot name="title">Untitled</slot></header>
<slot></slot>
</div>
`;
class InfoCard extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' })
.appendChild(template.content.cloneNode(true));
}
}
customElements.define('info-card', InfoCard);
</script>The <span> goes to the named title slot. The <p> has no slot attribute, so it goes to the default slot. If you remove the <span>, the header shows "Untitled".
Where this shows up in LWC
LWC does not ask you to create a <template> in JavaScript. Every component's HTML file has a root <template> element, and the compiler turns it into rendering code. Slots work the same way. You put <slot> and <slot name="title"> in a component's template, and a parent supplies content using the slot attribute. A later chapter covers composition in LWC.
The slot semantics come from the browser. The {expression} bindings, if:true and for:each are not part of the standard <template> element. The LWC compiler adds them.
Common errors
- Putting content inside
<template>and expecting it to appear. It stays inert until you clone it. - Using
innerHTMLon the shadow root with a<slot>in the string and then expecting the slot to work on a closed host. It works, but only if the shadow root is attached first. - Giving a child a
slotname that no slot defines. The child is not displayed. - Styling slotted content from inside the shadow tree with a plain selector. Use the
::slotted()pseudo-element, which matches only the direct slotted elements.
Exercise
In the example, a parent adds <p slot="footer">Thanks</p> inside <info-card>. The template has no slot named footer. What happens, and what change would make the paragraph appear?
Show answer
The paragraph stays in the light DOM but is not displayed, because no slot has the name "footer" to receive it. Adding <slot name="footer"></slot> to the component's template makes it appear there.