Conversation
Currently, we have to write `{{ status.value }}` to display an enum value.
Given that BackedEnums cannot implement `Stringable` / `__toString`, why not handle them in
the EscapeRuntime?
Now we can write `{{ status }}` and it will print the value.
|
Only handled in the Escaper would lead to many frustrations/incomprehensions and a weird DX, as it won't work for filters for instance... and that would require a lot of code in many places.. Let's say you have a string With your proposal, this would happen: ✅ {{ my_string }}
✅ {{ my_string|upper }}
✅ {{ my_stringable }}
✅ {{ my_stringable|upper }} *
✅ {{ my_enum }}
❌ {{ my_enum|upper }}Depending on the filters, call like the last one could either display the unchanged scalar value (with filters returning the value if not stringable) or throw an error (when type is checked).. with no predictability. But... for filter that require pre-escaped arguments, then the escaped scalar equivalent would be used. * So, as long PHP scalar backed enums are not (and cannot be) Stringable in PHP ... i don't think this should be done in Twig. |
|
Maybe a more natural way would be to implement this feature in |
|
@fabpot converting backed enums to their values when reading them in |
|
Let's close this, unless somebody has a good idea on how to solve it 😊 |
Let's say you have the following Enum:
Currently, we have to write
{{ status.value }}to display an enum value.Given that BackedEnums cannot implement
Stringable/__toString, why not handle them in the EscapeRuntime?Now we can write
{{ status }}and it will print the value.