Using Templates and Slots in Web Components: Crafting Dynamic and Flexible Components

Sudheer Kumar Reddy Gowrigari
2 min readOct 5, 2023

--

Web Components have heralded a new era in web development by offering a native way to create reusable and encapsulated components. One of the cornerstones of this technology is the ability to define templates and slots, which enables developers to design flexible content structures and render dynamic content. This article aims to provide an in-depth exploration of using templates and slots in Web Components, following the guidance of MDN’s extensive documentation.

The Power of Templates

In the realm of Web Components, the <template> element serves as a mechanism to declare fragments of markup that won't be rendered immediately but can be instantiated at runtime.

Key Points:

  • Templates are inert, meaning the content within them doesn’t render nor execute any scripts.
  • They serve as a blueprint for creating content dynamically.

Example:

<template id="my-template">
<p>This is a template. It won't be rendered until activated!</p>
</template>

Activating Templates

To bring a template to life, you must:

  1. Access the template using JavaScript.
  2. Use the document.importNode method to clone its content.
  3. Append the cloned content to the desired element.

Example:

const template = document.getElementById('my-template');
const clone = document.importNode(template.content, true);
document.body.appendChild(clone);

Slots: The Gateway to Content Projection

While templates provide a foundation, slots offer a dynamic way to project content into Web Components.

Understanding the <slot> Element:

  • Slots serve as placeholders within a Web Component’s shadow DOM where external content can be inserted.
  • They enable a mix of custom, predefined, and external content in Web Components.

Example of a Web Component with Slots:

class CustomCard extends HTMLElement {
constructor() {
super();
const shadowRoot = this.attachShadow({ mode: 'open' });
shadowRoot.innerHTML = `
<h2><slot name="card-title">Default Card Title</slot></h2>
<p><slot name="card-content">Default content goes here.</slot></p>
`;
}
}

customElements.define('custom-card', CustomCard);

Consuming a Component with Slots:

Developers can fill the slots by adding content with matching slot attributes.

Example:

<custom-card>
<span slot="card-title">My Custom Title</span>
<span slot="card-content">This is my custom card content!</span>
</custom-card>

Fallback Content and Default Slots

Slots can have fallback content, which gets displayed if no content is projected into the slot.

Example: Inside the Web Component:

<slot name="card-footer">Default footer message</slot>

If a consumer of the component doesn’t provide content for the card-footer slot, the "Default footer message" will be displayed.

Furthermore, if a <slot> element doesn't have a name attribute, it becomes a default slot, catching all content that doesn't have a designated slot.

Conclusion:

Templates and slots empower Web Components with flexibility and dynamism. By using templates, developers can define blueprints of content to be activated on demand. Meanwhile, slots offer an elegant way to mix and match predefined, custom, and external content within a component, ensuring both encapsulation and customization.

Mastering templates and slots is a vital step for any developer journeying through the world of Web Components. As we continue to explore the vast landscape of modern web development, understanding these foundational elements ensures the creation of robust, flexible, and user-friendly components.

--

--