Answers.Body: <p>Through further testing I found that if I FIRST include the {extrainfo.namearray} token (which simply prints 'array') and THEN run a loop through the contact array, it does in fact show all of the tokens!</p>
  
  <p>In the case anyone else finds this useful, my final code:</p>
  
  <pre><code>{extrainfo.namearray} // hide this so not seen in email
    {foreach name=outer from=$contact key=k item=it} 
       {if $k == 'extrainfo.namearray' }
         {foreach name=mid from=$it item=inn} 
             {$inn}
         {/foreach} 
       {/if}
    {/foreach}  
  </code></pre>
  
  <p>I'm not sure why I have to include the custom array first before it will include it in the contact array so that I can properly loop through it - but alas it's a workaround that works!!!</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-18T16:27:03.760Z
Answers.LastActivityDate: 2017-12-18T16:27:03.760Z
Answers.UserDisplayName: RayWright
Answers.Body: <p>The substitution of the email tokens is done by the <code>CRM_Utils_Token</code> class, not by smarty. But is possible to extend the smarty template variables using the following code.</p>
  
  <pre><code> $template = CRM_Core_Smarty::singleton();
   $template -&gt; assign('extrainfo',array(
     97485 =&gt; "Name 1"
     97486 =&gt; "Name 2"
     97547 =&gt; "Name 3"
   )
  </code></pre>
  
  <p>You have however to find the correct hook to add this code. A good candidate is maybe the civicrm_postProcess hook of the form that sends the email.</p>
  
  <p>Another, more generic, possibility to explore is to use <code>{crmApi}</code> smarty tag to use the Api to add more information to an email. Examples are generated by the API explorer.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-18T16:27:13.180Z
Answers.Text: While in this case I'm unable to affect the code that creates these tokens (I did find a work around, posted as a separate answer) are you saying that using the hook_civicrm_tokens is not enough? I should also assign the tokens a template?
Answers.UserDisplayName: RayWright
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-16T21:50:19.357Z
Answers.LastActivityDate: 2017-12-17T16:00:57.447Z
Answers.LastEditDate: 2017-12-17T16:00:57.447Z
Answers.UserDisplayName: Kainuk - CiviCooP
Body: <p>We've written a custom token to pull a piece of custom data off of any open cases. The token works as an imploded variable. I'd like to be able to use the token as an array as well (so I can run a for each loop in an email communication.) I am having trouble accessing the array.</p>
  
  <p>If I use the variable {extrainfo.namearray} the email prints "Array". I've tried a simple foreach loop</p>
  
  <pre><code>{foreach from=extrainfo.namearray item=value key=label}
      &lt;tr&gt;
      &lt;td&gt;{$label}&lt;/td&gt;
      &lt;td&gt;{$value}&lt;/td&gt;
      &lt;/tr&gt;
  {/foreach}
  </code></pre>
  
  <p>This prints out an "0" and "extrainfo.namearray" I can only assume that's becuase I don't have the "$" before the token name. The statement prints out nothing if I add the $ to the token name.</p>
  
  <p>I know the array has content, if I use {debug} in an email, I can see that it is part of the {$contact} tokens. It prints our the array itself with the expected data:</p>
  
  <pre><code>extrainfo.namearray =&gt; Array (3)
  97485 =&gt; "Name 1"
  97486 =&gt; "Name 2"
  97547 =&gt; "Name 3"
  </code></pre>
  
  <p>I also tried accessing it in this manner {$contact.extrainfo.namearray} but that prints nothing (not even "Array").</p>
  
  <p>Any advice on how I can target/loop items in the {extrainfo.namearray} array?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-12-07T21:16:13.587Z
Comments.Text: With some more testing, I've found that while the debug lists the {$contact} array with 23 items and includes my custom extrainfo.namearray (and several other related custom tokens). But when I loop through the {$contact} token in an email it only lists the standard contact tokens - about 8 of them - but does not list any of the custom tokens we've written. Any idea why a debug will list the custom tokens but a basic loop {foreach from=$contact item=value key=label} won't?
Comments.UserDisplayName: RayWright
Tags: <tokens><smarty>
Title: Smarty Custom Variable / Token Array
ThreadText: # Smarty Custom Variable / Token Array
  
  (Asked by: username_0 on 2017-11-20)
  
  We've written a custom token to pull a piece of custom data off of any open cases. The token works as an imploded variable. I'd like to be able to use the token as an array as well (so I can run a for each loop in an email communication.) I am having trouble accessing the array.
  If I use the variable {extrainfo.namearray} the email prints "Array". I've tried a simple foreach loop
  {foreach from=extrainfo.namearray item=value key=label}
      
      {$label}
      {$value}
      
  {/foreach}
  
  This prints out an "0" and "extrainfo.namearray" I can only assume that's becuase I don't have the "$" before the token name. The statement prints out nothing if I add the $ to the token name.
  I know the array has content, if I use {debug} in an email, I can see that it is part of the {$contact} tokens. It prints our the array itself with the expected data:
  extrainfo.namearray => Array (3)
  97485 => "Name 1"
  97486 => "Name 2"
  97547 => "Name 3"
  
  I also tried accessing it in this manner {$contact.extrainfo.namearray} but that prints nothing (not even "Array").
  Any advice on how I can target/loop items in the {extrainfo.namearray} array?
  
  **Tags:** tokens, smarty
  
  > **Comments:**
  > **username_0**: With some more testing, I've found that while the debug lists the {$contact} array with 23 items and includes my custom extrainfo.namearray (and several other related custom tokens). But when I loop through the {$contact} token in an email it only lists the standard contact tokens - about 8 of them - but does not list any of the custom tokens we've written. Any idea why a debug will list the custom tokens but a basic loop {foreach from=$contact item=value key=label} won't?  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2017-12-18. Score: 1 | Accepted answer
  
  Through further testing I found that if I FIRST include the {extrainfo.namearray} token (which simply prints 'array') and THEN run a loop through the contact array, it does in fact show all of the tokens!
  In the case anyone else finds this useful, my final code:
  {extrainfo.namearray} // hide this so not seen in email
    {foreach name=outer from=$contact key=k item=it} 
       {if $k == 'extrainfo.namearray' }
         {foreach name=mid from=$it item=inn} 
             {$inn}
         {/foreach} 
       {/if}
    {/foreach}  
  
  I'm not sure why I have to include the custom array first before it will include it in the contact array so that I can properly loop through it - but alas it's a workaround that works!!!
  
  
  ---
  
  ### Answer by username_1 on 2017-12-16. Score: 0
  
  The substitution of the email tokens is done by the CRM_Utils_Token class, not by smarty. But is possible to extend the smarty template variables using the following code.
   $template = CRM_Core_Smarty::singleton();
   $template -> assign('extrainfo',array(
     97485 => "Name 1"
     97486 => "Name 2"
     97547 => "Name 3"
   )
  
  You have however to find the correct hook to add this code. A good candidate is maybe the civicrm_postProcess hook of the form that sends the email.
  Another, more generic, possibility to explore is to use {crmApi} smarty tag to use the Api to add more information to an email. Examples are generated by the API explorer.
  
  > **Comments:**
  > **username_0**: While in this case I'm unable to affect the code that creates these tokens (I did find a work around, posted as a separate answer) are you saying that using the hook_civicrm_tokens is not enough? I should also assign the tokens a template?  
  
  ---
  
  