Reference / Symfony
/

Twig

Symfony's default templating engine. Auto-escapes output, supports inheritance, and compiles templates to PHP.

Foundational
  • Inheritance: `{% extends 'base.html.twig' %}` with `{% block name %}...{% endblock %}` overrides.
  • Includes: `{% include '_partial.html.twig' with { var: value } only %}`.
  • Auto-escaping is on by default — use `|raw` deliberately and only on trusted content.
  • Functions and filters are extension points: write a `TwigExtension` to add custom ones.
  • `{{ path('route_name', { id: 5 }) }}` generates URLs; `{{ asset('css/x.css') }}` for assets.
  • Macros (`{% macro ... %}`) are reusable blocks — like functions. Components (Symfony UX) are the modern replacement.

Common gotchas

  • `|raw` on user input is an XSS vulnerability — almost never the right answer.
  • Templates are cached aggressively in prod — clear the cache after deploy.
  • `only` on includes prevents the parent context from leaking in — use it to keep partials reusable.