Strict
Component ID
Component name
Component type
Maintenance status
Development status
Component security advisory coverage
Component created
Component changed
Component body
Strict is based on the Stark theme, but will not throw errors if strict_variables
is TRUE (unlike all core themes).
#1806538: Remove @todo about enabling strict_variables in Twig resolved that strict_variables
should remain at its default value of FALSE. Since then core themes have gotten sloppy to the point of incorrectly documenting which variables are optional and which are required. #2445705: Core themes should not throw exceptions when strict_variables is TRUE was an attempt to correct this in core, but was rejected for a number of reasons.
What are strict variables and why use them
In a Twig template, if a variable or attribute does not exist you will receive a null value when the strict_variables
option is set to false
; alternatively, if strict_variables
is true
, Twig will throw an error. This is useful for finding typos or copy/paste errors early in the dev cycle.
In addition, Drupal best practices recommend turning on error_reporting (in PHP) and adding "use strict" to Javascript files. Seems we should be able to do it in Twig templates as well!
How do I turn on strict_variables
?
Add the following line in your sites/[site_name|default]/services.yml
file just following the twig.config:
line (approx line 40):
strict_variables: true