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.
This commit is contained in:
Hugo Alliaume
2025-09-12 14:33:34 +02:00
parent 98b664a0ca
commit def4abbd5e
2 changed files with 11 additions and 4 deletions
+9 -3
View File
@@ -10,15 +10,21 @@
.. code-block:: twig
{# display one specific case of a backed enum #}
{{ enum('App\\MyEnum').SomeCase.value }}
{{ enum('App\\CardSuite').Clubs.value }} {# "clubs" #}
{# get all cases of an enum #}
{% for case in enum('App\\MyEnum').cases %}
{% 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\\MyEnum').someMethod() }}
{{ 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.
+2 -1
View File
@@ -9,9 +9,10 @@
.. code-block:: twig
{% for case in enum_cases('App\\MyEnum') %}
{% for case in enum_cases('App\\CardSuite') %}
{{ case.value }}
{% endfor %}
{# "clubs", "spades", "hearts", "diamonds" #}
When using a string literal for the ``enum`` argument, it will be validated during compile time to be a valid enum name.