Calling method in Twig, how to avoid the return showing as output

Viewed 42

I am calling a method in a pre-existing object from inside a Twig template, after properly authorizing it in policy:

{{ thisEmail.AddCC('me@example.com') }}

My problem is that the object method, which I use to produce a side-effect, not to return any text, returns true, and that leaves a visible "1" in my template output.

How to remove this output? I have this one working:

{{ thisEmail.AddCC('me@example.com')|trim('1') }}

But I feel it's not entirely elegant, and it can only remove that specific result. I would prefer to write something like |nul or |drop and have the filter drop everything, regardless of how much or what kind of stuff it receives as input.

I know how to do it by writing my own Twig extension filters, but my question here is: is there a nice way of achieving this with the core Twig language, un-extended?

1 Answers

If you want a twig statement not to output anything you can use the do tag, e.g.

{% do thisEmail.AddCC('me@example.com') %}

source

Related