File: guard.rst

package info (click to toggle)
php-twig 3.20.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,940 kB
  • sloc: php: 23,320; makefile: 110; sh: 43
file content (28 lines) | stat: -rw-r--r-- 767 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
``guard``
=========

.. versionadded:: 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.

.. code-block:: twig

    {% 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:

.. code-block:: twig

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