Twig’s _self variable identifies the current template; it is not an application-owned identifier. If a translation key, cache key or analytics label must remain stable across cache generation methods, give it an explicit value instead of deriving it from _self.
Last updated: November 18, 2026.
{# Stable across template moves, loaders and cache warmup paths. #}
{% set translation_prefix = 'account.profile' %}
<h1>{{ (translation_prefix ~ '.title')|trans }}</h1>
<p>{{ (translation_prefix ~ '.summary')|trans }}</p>The prefix now belongs to the application’s translation contract. A template rename or a different internal representation of its name cannot silently change the lookup key.
Why _self is the wrong durable identifier
The Twig template documentation defines _self as the current template name. The name depends on the template loader and how the application addresses the template; it is useful for template-aware behavior, but the documentation does not promise a normalized business identifier.
A current Symfony report shows an affected setup producing different slash styles when Twig cache files are generated by a browser request versus a CLI cache warmup. The report covers particular Symfony and Twig combinations and remains under review, so it should not be treated as proof that every version behaves identically. It does demonstrate why a representation supplied by the rendering system should not become a persistent translation or cache namespace.
Keep deployment cache generation consistent
Use explicit prefixes for translations, application cache entries, metrics and permissions. If many templates need the same prefix, pass it from a controller or expose a small, documented Twig global. Keep the value independent of physical template paths and directory separators.
Symfony’s deployment guide recommends clearing and warming the production cache with APP_ENV=prod and debugging disabled. Run that step under a user that can write the cache directory, and use the same release, dependencies and environment variables that serve requests. After deployment, render a representative page and verify the expected translation keys. Clearing the cache may remove a stale mismatch, but it does not make an _self-derived business key a stable design.