I am having issues with twig escaping output on method calls to an object passed into my twig template. I know I can get around this with the |raw filter, but was hoping there might be a way I could simply specify in my object that certain methods are safe and therefore remove the need for the raw filter.
I am having issues with twig escaping output on method calls to an object
Share
Method calls of objects itself cannot be made html safe because normal objects/entities are not (and should not be) aware of the template engine.
However, a twig filter or function if aware of the template engine and can be marked html safe in its definition.
So what you need to do is to implement a html safe twig filter to pass the object to and call the method of your object inside the filter function.
I guess your templates looks like this:
Now you need to implement a twig filter and change the template to the following:
And the twig extension should look like this:
One design consideration: If your object is an entity of a business object of some kind, it sould not create html but you should move the html creation to a template or the twig filter..