Theming

Use a Component in a Twig Template for Drupal 10, 11

Last updated
Categories
Up-to-date with minor version
11.0.x

Learn how to embed a Drupal single directory component (SDC) in a Twig template like node.html.twig. This tutorial explains how to pass props and slots to the component, how to determine the correct namespace and name, and how to choose between Twig’s {% include %} and {% embed %} when rendering an SDC. This technique is most often used by theme developers who override templates and compose a page using single directory components. A common scenario is expressing your design system as components in a theme, then overriding templates to use those components.

In this tutorial, we will:

  • Reference a component from a parent Twig template file.
  • Pass props and slot values to a component from that template.
  • Decide when to use {% include %} or {% embed %} based on the component's slot structure.

By the end of this tutorial, you’ll be able to embed a component into a Twig template and pass values to both its props and slots.

Frontend Theming