Twig

The flexible, fast, and secure
template engine for PHP

a Symfony Product
Docs Tags guard
You are reading the documentation for Twig 3.x. Switch to the documentation for Twig 1.x, 2.x.

Questions & Feedback

License

Twig documentation is licensed under the new BSD license.

guard

3.15

The guard tag was added in Twig 3.15.

The guard statement checks if some Twig callables are available at compilation time to bypass code compilation that would otherwise fail.

1
2
3
{% guard function importmap %}
    {{ importmap('app') }}
{% endguard %}

The first argument is the Twig callable to test: filter, function, or test. The second argument is the Twig callable name you want to test.

You can also generate different code if the callable does not exist:

1
2
3
4
5
{% guard function importmap %}
    {{ importmap('app') }}
{% else %}
    {# the importmap function doesn't exist, generate fallback code #}
{% endguard %}