Files
Twig/doc/functions/enum.rst
T
Hugo Alliaume def4abbd5e Improve documentation examples for enum and enum_cases
It may be obvious, but I first thought initializing an BackendEnum with a certain value could be possible with `enum(FQCN, value)`, but it does not, it always returns the first case  instance (if exists).

Instead, `enum(FQCN).from(value)` must be used, I think it's worth to improve `enum()` examples.
2025-09-12 18:14:50 +02:00

35 lines
908 B
ReStructuredText

``enum``
========
.. versionadded:: 3.15
The ``enum`` function was added in Twig 3.15.
``enum`` gives access to enums:
.. code-block:: twig
{# display one specific case of a backed enum #}
{{ enum('App\\CardSuite').Clubs.value }} {# "clubs" #}
{# get all cases of an enum #}
{% for case in enum('App\\CardSuite').cases %}
{{ case.value }}
{% endfor %}
{# "clubs", "spades", "hearts", "diamonds" #}
{# get a specific case of an enum by value #}
{% set card_suite = enum('App\\CardSuite').from('hearts') %}
{{ card_suite.name }} {# "Hearts" #}
{{ card_suite.value }} {# "hearts" #}
{# call any methods of the enum class #}
{{ enum('App\\CardSuite').someMethod() }}
When using a string literal for the ``enum`` argument, it will be validated during compile time to be a valid enum name.
Arguments
---------
* ``enum``: The FQCN of the enum