Files
Twig/doc/functions/enum.rst
T
Fabien Potencier b03de9de05 Merge branch '3.x' into 4.x
* 3.x:
  Update CHANGELOG
  Fix accessing arrays with stringable objects as key
  Update inky_to_html.rst: Updating link
  Update replace.rst
  [Doc] Tweaks in the escaping article
  Compile 'index' with repr (not string) in EmbedNode
  Introduce registerUndefinedTestCallback
  Fix intl test
  Bump minimum Commonmark requirement
  Support two words test guard
  Bump version
  Improve documentation examples for `enum` and `enum_cases`
  Avoid errors when failing to guess the template info for an error
  Add note to format_datetime explaining how to install required extensions
  Fix compatibility layer
2025-10-29 11:55:46 -04:00

32 lines
833 B
ReStructuredText

``enum``
========
``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