I want to provide a default value for tokens when the value is empty, on a per-use basis -- e.g., something like {contact.first_name?Friend} would print the contact's First Name if it's not empty, otherwise print "Friend".

Rationale update: This would allow non-programmers to easily specify a default value without needing to clutter up the tokens UI.

Existing workarounds for this functionality are more cumbersome than that:

  • Dave's blog from version 2.2 uses hooks in an extension to hard-code defaults for specific tokens in all cases.
  • This SE answer points out that this can be achieved by enabling Smarty processing and then inserting a few lines -- per token -- of Smarty code into the content.

To avoid double-work and/or undertaking the impossible, I ask:

Has any attempt been made to put such per-use-default-value functionality into CiviCRM core or an extension?

Technical challenges update: Tokens are limited to \w characters, so any plan to use a non-word delimiter like ? will have to involve changes in core, as far as I can see.

  • I think the Smarty solution is simple enough. If you want it be on a per-use basis, I don't see how you can do it elsewhere without adding significant functionality to tokens.
    – Mick Kahn
    Nov 21, 2019 at 21:39
  • I do like your proposal. Nov 22, 2019 at 8:20

1 Answer 1


First - no need to enable Smarty system-wide to do this. Greetings are always parsed by Smarty - see https://civicrm.org/blog/antrik/getting-started-with-new-name-and-greetings-features-in-45.

Besides that, I think you're one of the leading experts on complex token extensions, since you're the primary author of reltoken. But I don't see why you couldn't do this. hook_civicrm_tokenValues gives you access to the token name for parsing, and there's no requirement that the token be listed with hook_civicrm_tokens. So I'd say go for it.

Note that this is similar but not identical to the free-form relative date work Eileen did: https://github.com/civicrm/civicrm-core/pull/12682.

  • Tokens are limited to [\w] characters, so any plan to use a non-word delimiter like ? will have to involve changes in core. I'm concerned that will mushroom into something more than I want to undertake.
    – TwoMice
    Nov 21, 2019 at 23:01

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.