09/27/2026
Twig strict_variables: errors or silent null
strict_variables decides what Twig does with a variable, attribute or method that doesn't exist. Set to false, Twig quietly treats it as null. Set to true, it throws an error. Plain Twig defaults to false. Symfony defaults it to %kernel.debug%, so it's on in dev and off in prod.
Setting it
In Symfony, in config/packages/twig.yaml:
twig:
strict_variables: true
In plain Twig, when you create the environment:
$twig = new \Twig\Environment($loader, [
'strict_variables' => true,
]);
Both are documented: Twig's environment options and Symfony's Twig configuration reference.
Why a template works locally and misbehaves in production
With Symfony's default, a typo such as {{ user.nmae }} throws in dev, where you'd notice it, and prints nothing in prod, where you won't. That split is deliberate, and it's usually the right one. Turning it off in dev hides typos. Turning it on in prod makes a missing optional field an error page for visitors.
Handling a value that may be missing
With strict variables on, say what should happen when a value is absent:
{{ user.nickname ?? user.name }}
{{ subtitle|default('') }}
{% if promo is defined %}{{ promo.code }}{% endif %}
?? returns the left side if it's defined and not null. default returns its argument if the value is undefined or empty. Twig's defined test is described as "very useful if you use the strict_variables option".
One difference matters: default treats false and empty strings as missing too. Twig's default filter page suggests ?? when false is a real value.
What each form returns, tested
Tested on Twig 3.30.0 on PHP 8.3, rendering the templates below with user set to ['name' => 'Ada'] and flag set to false, once with strict_variables off and once on:
| Template | Off | On |
|---|---|---|
Hello {{ user.nmae }}! |
Hello ! |
Twig\Error\RuntimeError: Key "nmae" for sequence/mapping with keys "name" does not exist |
{{ user.nickname ?? user.name }} |
Ada |
Ada |
{{ subtitle|default("") }} |
empty | empty |
{% if promo is defined %}...{% endif %} |
empty | empty |
{{ flag|default("fallback") }} |
fallback |
fallback |
{{ flag ?? "fallback" }} |
empty (false prints nothing) |
empty |
The last two rows are the difference described under "Handling a value that may be missing": default replaced the false, ?? kept it.
Where strict mode throws that you might not expect
Same setup, with user an object that has a name property, a profile property set to null and a title() method:
| Template | Off | On |
|---|---|---|
{{ user.profile.bio }} |
empty | RuntimeError: Impossible to access an attribute ("bio") on a null variable |
{{ user.profile.bio ?? "none" }} |
none |
none |
{{ user.nope }} |
empty | RuntimeError: Neither the property "nope" nor one of the methods "nope()", "getnope()", ... exist |
{% if missing %}y{% else %}n{% endif %} |
n |
RuntimeError: Variable "missing" does not exist |
{% if missing is null %}...{% endif %} |
y |
RuntimeError: Variable "missing" does not exist |
{% if user.profile is defined %}... |
yes |
yes |
{% for x in items %}...{% endfor %} |
no output | RuntimeError: Variable "items" does not exist |
{% for x in items|default([]) %}... |
no output | no output |
{{ missing|upper }}, {{ missing|length }} |
empty, 0 |
RuntimeError |
Three of these are the ones that catch people. A plain {% if missing %} is not safe with strict variables on, because Twig has to read the variable to test it, and missing is null fails for the same reason. Use is defined, ?? or |default for a value that may not be there. And is defined answers yes for an attribute that exists and holds null, so it does not tell you the value is usable. Use ?? for that.
How to verify
Put a deliberate typo in a template and load the page in dev. You should get an error page naming the missing attribute. In prod the same page renders with a gap, which is the behavior worth knowing about before it happens. The Twig cheat sheet covers the rest of the syntax.
Questions this raises
What is the default value of strict_variables?
false in plain Twig. In Symfony it defaults to %kernel.debug%, so it's true in dev and false in prod.
Does is defined work when strict_variables is on?
Yes. Twig's documentation describes the defined test as very useful with strict_variables, and ?? and the default filter also handle undefined values without throwing.
Does strict_variables cover methods and array keys, or only variables?
It covers variables and attributes. In Twig an attribute is whatever a dot or square brackets reach: an object property or method, or an array key.
Should I turn strict_variables on in production?
Usually not. In production a missing optional value would become an error page for visitors. Keep it on in development and tests, where an error is what you want.
Researched and drafted with AI, then reviewed and edited by the site's editors.