Answers.Body: <p>We used the Dave's Blog <a href="https://civicrm.org/blog/dave-greenberg/civimail-how-to-add-default-values-for-empty-tokens" rel="nofollow noreferrer">https://civicrm.org/blog/dave-greenberg/civimail-how-to-add-default-values-for-empty-tokens</a> </p>
  
  <p>The blog provides a sample code and the hook which need to be used. We added the same <code>tokenValues()</code> hook to the extension and it works great in replacing all the empty token values with the one specified in the hook.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-20T02:17:10.613Z
Answers.Text: if someone wants the code just sing out.
Answers.UserDisplayName: petednz - fuzion
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-26T16:19:59.373Z
Answers.Text: Is the extension you made released publicly?
Answers.UserDisplayName: Laryn
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-14T07:14:22.903Z
Answers.LastActivityDate: 2018-03-14T07:14:22.903Z
Answers.UserDisplayName: jitendra - fuzion
Answers.Body: <p>If you enable the Smarty template engine for mails with <code>define('CIVICRM_MAIL_SMARTY', 1);</code> you can use it to to handle defaults on a per mailing basis:
  <code>
  {capture assign=first_name}{contact.first_name}{/capture}
  Dear {$first_name|default:Friend},</code></p>
  
  <p>see also: <a href="https://docs.civicrm.org/user/en/latest/common-workflows/tokens-and-mail-merge/#smarty" rel="nofollow noreferrer">https://docs.civicrm.org/user/en/latest/common-workflows/tokens-and-mail-merge/#smarty</a>) </p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-13T18:43:51.677Z
Answers.Text: 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
Answers.UserDisplayName: petednz - fuzion
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-02-26T15:21:19.770Z
Answers.Text: Really helpful answer. Thank you. Certainly seems the easiest way to set up default replacement values.
Answers.UserDisplayName: Ben
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-13T12:16:40.473Z
Answers.LastActivityDate: 2018-03-13T12:16:40.473Z
Answers.UserDisplayName: Thomas Schüttler
Answers.Body: <p>One workaround is to <a href="https://civicrm.org/blog/colemanw/create-your-own-tokens-for-fun-and-profit" rel="nofollow noreferrer">define your own token</a> that handles nulls and defaults as you want.  But I agree it would be useful to have a non-code way to implement defaults.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-08T11:27:08.560Z
Answers.LastActivityDate: 2018-03-08T11:27:08.560Z
Answers.UserDisplayName: Aidan
Answers.Body: <p>Since people are looking at this question, it's worth noting that you don't need to enable Smarty system-wide to do this for greetings.  CiviCRM 4.5+ automatically handles Smarty in the greetings fields, see this blog post: <a href="https://civicrm.org/blog/antrik/getting-started-with-new-name-and-greetings-features-in-45" rel="nofollow noreferrer">https://civicrm.org/blog/antrik/getting-started-with-new-name-and-greetings-features-in-45</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-21T22:05:47.747Z
Answers.LastActivityDate: 2019-11-21T22:05:47.747Z
Answers.UserDisplayName: Jon G - Megaphone Tech
Body: <p>Has anyone developed an extension to handle null values in tokens?</p>
  
  <p>For example:</p>
  
  <p>Sending an email to a contact with no first name:</p>
  
  <p>Dear {first.name}, will end up as  -> Dear ,</p>
  
  <p>It appears that <a href="https://wiki.civicrm.org/confluence/display/CRMDOC33/Mail+Generator#MailGenerator-Nullvaluesanddefaults" rel="nofollow noreferrer">previously in 3.3</a> it was possible to use {first.name | default text} </p>
  
  <p>Additionally <a href="https://forum.civicrm.org/index.php?topic=19133.0" rel="nofollow noreferrer">a long while ago</a> there was some discussion about reviving this behaviour. </p>
  
  <p>Following this there was <a href="https://civicrm.org/blog/dave-greenberg/civimail-how-to-add-default-values-for-empty-tokens" rel="nofollow noreferrer">this post</a> 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. </p>
  
  <p>Following that I can only spot this <a href="https://civicrm.stackexchange.com/questions/17521/mailmerge-token-syntax/17593#17593">SE question</a> referencing the issue last year. </p>
  
Tags: <civimail><tokens>
Title: Handling null values in tokens in civimail (and elsewhere)
ThreadText: # Handling null values in tokens in civimail (and elsewhere)
  
  (Asked by: username_0 on 2018-03-08)
  
  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](https://wiki.civicrm.org/confluence/display/CRMDOC33/Mail+Generator#MailGenerator-Nullvaluesanddefaults) it was possible to use {first.name | default text}
  
  Additionally [a long while ago](https://forum.civicrm.org/index.php?topic=19133.0) there was some discussion about reviving this behaviour.
  
  Following this there was [this post](https://civicrm.org/blog/dave-greenberg/civimail-how-to-add-default-values-for-empty-tokens) 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](https://civicrm.stackexchange.com/questions/17521/mailmerge-token-syntax/17593#17593) referencing the issue last year.
  
  **Tags:** civimail, tokens
  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_1 on 2018-03-14. Score: 3 | Accepted answer
  
  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.
  
  > **Comments:**
  > **username_2**: if someone wants the code just sing out.  
  > **username_3**: Is the extension you made released publicly?  
  
  ---
  
  ### Answer by username_4 on 2018-03-13. Score: 2
  
  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>)
  
  > **Comments:**
  > **username_2**: THanks username_4 - 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  
  > **username_5**: Really helpful answer. Thank you. Certainly seems the easiest way to set up default replacement values.  
  
  ---
  
  ### Answer by username_6 on 2018-03-08. Score: 1
  
  One workaround is to [define your own token](https://civicrm.org/blog/colemanw/create-your-own-tokens-for-fun-and-profit) that handles nulls and defaults as you want. But I agree it would be useful to have a non-code way to implement defaults.
  
  
  ---
  
  ### Answer by username_7 on 2019-11-21. Score: 1
  
  Since people are looking at this question, it's worth noting that you don't need to enable Smarty system-wide to do this for greetings. CiviCRM 4.5+ automatically handles Smarty in the greetings fields, see this blog post: <https://civicrm.org/blog/antrik/getting-started-with-new-name-and-greetings-features-in-45>
  
  
  ---
  
  