Skip to content

Latest commit

History

History
33 lines (25 loc) 路 1.34 KB

enum_constants_in_templates.md

File metadata and controls

33 lines (25 loc) 路 1.34 KB

ENUM constants in templates

There is also another custom TWIG filter |enum_constant. It allows to use constants from ENUM classes in templates to print their values or to compare with other values.

{{ 'SHOOTING_GUARD'|enum_constant }}
{{ 'NORTH_WEST'|enum_constant }}

{% if player.position == 'SHOOTING_GUARD'|enum_constant %}
    <span class="custom-class">{{ player.position }}</span>
{% endif %}

Same problem as for |readable_enum filter is present here too. If some constant is defined in few ENUM classes then an exception will be thrown. You can specify the correct class for this constant and it solves the problem.

{{ 'CENTER'|enum_constant('BasketballPositionType') }}
{{ 'CENTER'|enum_constant('MapLocationType') }}

More features