Answers.Body: <p>That's really strange! Just a guess but maybe it's a problem with the way <code>$this-&gt;assign</code> works in message templates? I've only tested that construct in smarty <em>pages</em> and message templates might have a different way of processing variables like that.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-03-14T16:34:17.187Z
Answers.Text: Yeah, I've asked around and apparently one can't use {php} tags within message templates. I got round the problem eventually by creating a custom token in an extension.
Answers.UserDisplayName: William Gordon
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-03-10T21:34:28.113Z
Answers.LastActivityDate: 2017-03-10T21:34:28.113Z
Answers.UserDisplayName: Coleman
Body: <p>I'm trying to use the CiviCRM API to show a list of upcoming events (in the confirmation email a user receives when they sign up for an event). </p>
  
  <p>When I try the operation in the API Explorer it appears to return exactly what I want, but when I apply the code (in this case, the Smarty code) in the Message Template, the filtering to only show events after the date specified doesn't seem to take any effect. It just shows all the events.</p>
  
  <pre><code>{php}
    $this-&gt;assign("param_start_date", array('&gt;' =&gt; "2013-10-22 18:00:00"));
    $this-&gt;assign("param_options", array('sort' =&gt; "start_date"));
  {/php}
  {crmAPI var='result' entity='Event' action='get' return="title,start_date" start_date=$param_start_date options=$param_options}
  {foreach from=$result.values item=event}
    {$event.event_title}
  {/foreach}
  </code></pre>
  
  <p>I got the above code using the API Explorer by:</p>
  
  <ul>
  <li>Selecting 'Event' as the entity. </li>
  <li>'Get' as the method. 'Event Title'
  and 'Event Start Date' as the Fields to return. </li>
  <li>'Event Start Date' as
  the parameter, with '>' as the operator and '2013-10-22 18:00:00' as
  the value.</li>
  <li>Selecting 'Sort' as an option, with value 'start_date'.</li>
  </ul>
  
Tags: <civievent><api>
Title: Why does this use of the API for Events work in the Explorer, but not in the Message Template?
ThreadText: # Why does this use of the API for Events work in the Explorer, but not in the Message Template?
  
  (Asked by: username_0 on 2017-03-09)
  
  I'm trying to use the CiviCRM API to show a list of upcoming events (in the confirmation email a user receives when they sign up for an event). 
  When I try the operation in the API Explorer it appears to return exactly what I want, but when I apply the code (in this case, the Smarty code) in the Message Template, the filtering to only show events after the date specified doesn't seem to take any effect. It just shows all the events.
  {php}
    $this->assign("param_start_date", array('>' => "2013-10-22 18:00:00"));
    $this->assign("param_options", array('sort' => "start_date"));
  {/php}
  {crmAPI var='result' entity='Event' action='get' return="title,start_date" start_date=$param_start_date options=$param_options}
  {foreach from=$result.values item=event}
    {$event.event_title}
  {/foreach}
  
  I got the above code using the API Explorer by:
  
  Selecting 'Event' as the entity. 
  'Get' as the method. 'Event Title'
  and 'Event Start Date' as the Fields to return. 
  'Event Start Date' as
  the parameter, with '>' as the operator and '2013-10-22 18:00:00' as
  the value.
  Selecting 'Sort' as an option, with value 'start_date'.
  
  **Tags:** civievent, api
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-03-10. Score: 0 | Accepted answer
  
  That's really strange! Just a guess but maybe it's a problem with the way `$this->assign` works in message templates? I've only tested that construct in smarty *pages* and message templates might have a different way of processing variables like that.
  
  > **Comments:**
  > **username_0**: Yeah, I've asked around and apparently one can't use {php} tags within message templates. I got round the problem eventually by creating a custom token in an extension.  
  
  ---
  
  