Twig
Twig copied to clipboard
Mark safe object based on prop/method
Hi, I need to be able to mark an object as safe based on a prop value.
Following the docs, I can successfully add a safe class/interface. However, the use case is different. For example:
Field $a has safeHtml prop set to false -> not safe
Field $b has safeHtml prop set to true -> should be safe (html rendered without using |raw in Twig itself).
I've looked at the code and PR that implements it, and tried the following:
- Custom escaper -> does not work, because by the time the escaper is called, the value is already cast to string, since the object implements
__toString(). Hence, there's no longer access tosafeHtml - If the object does not implement
__toString()then things like{{ a ~ ' something else' }}fail (aandbcannot be cast to string) - Overriding
htmlescaper -> does not work, since it's behaviour is hard-coded (see PR linked above)
Any guidelines to what can be done to support this? Thanks.
Twig supports making some classes as safe regarding auto-escaping (Twig\Markup is always like that, but custom classes or interfaces can be registered too). But it does not have a way to make this safety conditional based on a property
hi @stof, thanks for the info! Do you have any pointers of what needs to be done to get this to work? Either downstream in the app I'm working on, or as a PR in twig itself?
Would it make sense to create a VO that encapsulates "safe" markup? So that if you pass in a value of this type, it won't be escaped?
such VO already exist. What does not exist is the VO encapsulating "sometimes-safe" markup
Sorry @stof and @apfelbox but... what is a VO? 🙈
a value object