Check Errors in Twig Templates using Console Command in Symfony 7

Check Errors in Twig Templates using Console Command in Symfony 7

Symfony framework provides various static code analysis commands (also known as linters) for checking some parts of application such YAML syntax, service definitions in container, etc.

This tutorial shows how to check errors in Twig templates using console command in Symfony 7 application.

Let's say we have the following template:


{% Hello %}

The lint:twig command can be used to check if Twig templates don't have any syntax errors. We can provide template filename as argument:

php bin/console lint:twig templates/test/index.html.twig

If the template is valid then the command prints a success message, otherwise an error is printed:

ERROR  in templates/test/index.html.twig (line 1) 
>>  1      {% Hello %}
>> Unknown "Hello" tag.

A command also accepts directory name as argument to check a whole directory:

php bin/console lint:twig templates

The problem can be fixed by using {{ ... }} syntax to print string:


{{ 'Hello' }}

A command has --show-deprecations option that allows to display the deprecated features that found in the templates.

php bin/console lint:twig --show-deprecations templates

If continuous integration is used, recommended adding lint:twig command to the list of commands which executed on each build.

Leave a Comment

Cancel reply

Your email address will not be published.