Skip to content

Drupal Twig Theming

I need to... Guide Summary
Understand how Twig templates work in Drupal Twig Overview When you need to understand how the Twig template engine integrates with Drupal's theme system before diving into specific template work.
Name my template file correctly Template Discovery & Naming When you need to create a template file and need to name it correctly, or when your template is not being picked up by Drupal.
Know what templates exist in what order Template Hierarchy When you need to understand the full rendering chain — which template wraps which — and where to override at each level.
Know what variables are available in html/page/node/field templates Variables by Template Level When you need to know exactly what variables are available in a specific template type.
Access a field value in a Twig template Accessing Field Values This is the most critical section for day-to-day theming. Whenever you need to output, check, or manipulate field data in a Twig template.
Follow entity references (node → media → image) Entity Reference Traversal When a field references another entity (taxonomy term, media, user, paragraph, another node) and you need to access the referenced entity's data in Twig.
Loop over multi-value fields Multi-Value Fields When a field is configured to allow multiple values and you need to iterate, count, slice, or conditionally access individual items.
Add/remove CSS classes and HTML attributes The Attribute System When you need to add, remove, or modify HTML attributes in a template — particularly CSS classes, data attributes, ARIA attributes, and custom element attributes.
Write a preprocess function Preprocess Functions When you need to prepare data for templates, pass custom variables, modify existing variables, or manipulate the rendering pipeline before a template renders.
Pass custom data from PHP to a template Adding Variables in Preprocess When you need to pass data from PHP to a Twig template that is not available through the default variable set.
Register a new theme hook Theme Hooks & Registration When your module or theme needs to register a new template (not override an existing one), define the variables it accepts, and specify the default template file.
Add or override template suggestions Template Suggestions When you need Drupal to use a more specific template for certain conditions (specific node type, view mode, user role, etc.) without overriding the base template for all cases.
Use url(), path(), link(), file_url(), icon(), dump() in Twig Drupal Twig Functions When you need to generate URLs, attach libraries, create links, or use other Drupal-specific functions from within a Twig template.
Use |t, |without, |clean_class, |render, |striptags, and 30+ Twig native filters in Twig Drupal Twig Filters When you need to transform, escape, translate, or manipulate variables in Twig templates.
Use twig_tweak to render a view/block/entity Twig Tweak Module When you need to render a view, block, entity, or field from within a Twig template without a preprocess function — particularly useful for CMS-style layouts and quick embeds.
Include an SDC component from a Twig template SDC in Templates When including or rendering Single Directory Components from Twig templates, or when a component needs to embed another SDC component.
Debug which template is being used Template Debugging When you need to identify which template is rendering, what variables are available, or why output looks wrong.
Use extends, include, embed Template Inheritance When you want to share template structure across multiple templates, or include a sub-template/component as part of a larger template.
Know security and performance rules Security & Performance When writing or reviewing Twig templates and preprocess functions — these rules apply to all template work.
Avoid common Twig mistakes Anti-Patterns Code review checklist. Review your templates against these patterns before committing.
Find the relevant core source files Code Reference Map
Check source references and maintenance notes Sources & Maintenance