Twig Template Inheritance: Include, Embed, and Extends

Twig, PHP, Templates, Backend, Symfony

Twig Template Inheritance: Include, Embed, and Extends Diagram

Twig Template Inheritance: Include, Embed, and Extends

When working with Twig templates, three powerful tags help you structure and reuse code efficiently: extends, include, and embed. This article explains how they work, when to use each, and how they relate to one another.


Quick Summary

extends


🔹 include


embed


Relationship Diagram

Template

├── extends (only once, at the very beginning)
│   │
│   └── Blocks (defined in the extended layout)
│       │
│       ├── include (allowed anywhere)
│       └── embed (allowed anywhere)
│           │
│           ├── block overrides
│           └── include (allowed)

└── include (works directly in a template, even without extends)

Key Takeaways

By understanding how these three tags interact, you can build Twig templates that are both modular and easy to maintain.