Has anyone developed an extension to handle null values in tokens?

For example:

Sending an email to a contact with no first name:

Dear {first.name}, will end up as -> Dear ,

It appears that previously in 3.3 it was possible to use {first.name | default text}

Additionally a long while ago there was some discussion about reviving this behaviour.

Following this there was this post about how to change the behaviour site wide, however to enable more personalised emails (both in terms of the sender and recipient) would be great to allow for this to be done on a per email basis as well.

Following that I can only spot this SE question referencing the issue last year.


We used the Dave's Blog https://civicrm.org/blog/dave-greenberg/civimail-how-to-add-default-values-for-empty-tokens

The blog provides a sample code and the hook which need to be used. We added the same tokenValues() hook to the extension and it works great in replacing all the empty token values with the one specified in the hook.


One workaround is to define your own token that handles nulls and defaults as you want. But I agree it would be useful to have a non-code way to implement defaults.


If you enable the Smarty template engine for mails with define('CIVICRM_MAIL_SMARTY', 1); you can use it to to handle defaults on a per mailing basis: {capture assign=first_name}{contact.first_name}{/capture} Dear {$first_name|default:Friend},

see also: https://docs.civicrm.org/user/en/latest/common-workflows/tokens-and-mail-merge/#smarty)

  • THanks Thomas - good to know. we have gone ahead and done this via an extension now (though it doesn't provide a UI for setting this), but I will aim to add a blog about it in case it helps others – petednz - fuzion Mar 13 '18 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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