mirror of
https://github.com/twigphp/Twig.git
synced 2026-10-04 19:07:11 +00:00
b03de9de05
* 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
32 lines
833 B
ReStructuredText
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
|