Check Errors in Twig Templates using Console Command in Symfony 6

Check Errors in Twig Templates using Console Command in Symfony 6

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 6 application.

Let's say we have the following template:

templates/test/index.html.twig

{% 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 template is valid then command prints success message, otherwise 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

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

templates/test/index.html.twig

{{ '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 to add 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.