3

I know what the null coalescing operator (??) does. I have an example here with 3 operators and I am unsure what it does?

Here is an example that I came across in a script I am trying to debug:

{% set eventsButtonLabel = event.eventsButtonLabel ??? 'Book Ticket'|t ??? null %}

What is the function of 3 operators?

1 Answer 1

3

The ??? operator has been dubbed the "empty coalesce" operator. It's similar to the "null coalesce" operator (??), but ignores empty strings ("") and empty arrays ([]) as well.

The empty coalescing operator isn't part of Craft (or Twig) core, which means your site likely has the Empty Coalesce or SEOmatic plugins installed; both of these plugins add this operator to Twig.

If you want to avoid using that non-standard operator, it can be replaced with the |default Twig filter:

{% set eventsButtonLabel = event.eventsButtonLabel|default('Book Ticket'|t) %}

(That last ??? null in your example seems redundant, unless there is a static translation for 'Book Ticket' that is explicitly set to an empty string, and you specifically need the eventsButtonLabel variable to be null if that is the case).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.