Files
Fabien Potencier 16a7e4ec0d Merge branch '3.x' into 4.x
* 3.x:
  Document how to customize the markdown_to_html converter
  Make the include() function return a Markup object
  Fix nested block() resolution when a directly rendered block calls parent()
  Fix nested block() resolution when a directly rendered block calls parent()
  Document storing an enum in a variable to avoid repeating its FQCN
  Stop reporting a skipped test in IntegrationTestCase when there is no legacy test to run
  Document {#--#} as the replacement for the deprecated spaceless filter
  Skip the string cast in PrintNode when the expression is already a string and add tests
  Ensure PrintNode is yielding string content
  CoreExtension::getAttribute: small improvement regarding getter/isser/hasser

# Conflicts:
#	CHANGELOG
#	tests/Node/ForTest.php
2026-06-06 08:48:52 +02:00

45 lines
1.2 KiB
ReStructuredText

``enum``
========
``enum`` gives access to enums:
.. code-block:: twig
{# display one specific case of a backed enum #}
{{ enum('App\\CardSuite').Clubs.value }} {# "clubs" #}
{# display one specific case of a backed enum, with a dynamic name #}
{% set case_name = 'Spades' %}
{{ enum('App\\CardSuite').(case_name).name }} {# "Spades" #}
{# 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.
As the FQCN can be long, you can store the enum in a variable to avoid repeating
it:
.. code-block:: twig
{% set suite = enum('App\\CardSuite') %}
{{ suite.Clubs.value }} {# "clubs" #}
{{ suite.Spades.value }} {# "spades" #}
Arguments
---------
* ``enum``: The FQCN of the enum