Altering Email template in CiviCRM module of Drupal

Altering Email template in CiviCRM module of Drupal

Submitted by Pramod Jain on Thu, 03/10/2011 - 09:37
civiCRM logo

CiviCRM uses a server-side scripting language called Smarty in order to access server-side variables, perform logic and work with a template to generate a webpage.  The overall flow is:  All the business logic is done by PHP and all the presentation logic is done by Smarty.   So the PHP pages do the database updates and selects, access session variables, create data structures particular webpage or email template and then hand over the data structures to a Smarty page.  By the way, Smarty allows embedding of PHP code inside a webpage. Reader may wonder what the need for two scripting languages.  The reason is that CiviCRM standalone was developed using Smarty and when it was integrated with Drupal, the frontend stayed with Smarty. In this article, a simple task will be performed to illustrate the working of Smarty.  The task is to change an email template that is tied to event registration.  The change involved merging data elements.  Lets start with an existing piece of code in the email template:

 

{if $amount && !$lineItem}
    {foreach from=$amount item=amnt key=level}
        <tr><td colspan="2" {$valueStyle}>  {$amnt.amount|crmMoney} <br><h3> {$amnt.label|regex_replace:"/(- \d+)/":"\1</h3><br><h3>"}
        </h3></td></tr>
{/foreach}
{/if}

Assume a scenario in which a user registers for an event and purchases 4 tickets for the entire family. In the above code, a row is generated for each person in the family that is registered for the event.  Data is extracted from the array: $amount[i]["amount"], $amount[i]["label"], where i=0,1,2,3.  Smarty code is written inside {...}.  amount contains the dollar value of ticket (there are different types of tickets--VIP, normal, etc.--of different amounts). Later part of the email template contained the following code.  Notice the comments {* ... *} that indicate the change made to the code:     

     {if $customProfile}

      {foreach from=$customProfile item=value key=customName}
       <tr>
        <th {$headerStyle}>
         {ts 1=$customName+1}Participant %1{/ts}.  
 {*  THIS CODE HAS BEEN ADDED ... START *}
 {$amount[$customName].label}. Amount paid: {$amount[$customName].amount|crmMoney}
 {*  END *} 
        </th>
       <tr>
  {* Loop for each member of family on the ticket *} 
       {foreach from=$value item=val key=field}
        {if $field eq 'additionalCustomPre' or $field eq 'additionalCustomPost'}
         <tr>
          <td colspan="2" {$labelStyle}>
           {if $field eq 'additionalCustomPre'}
            {$additionalCustomPre_grouptitle}
           {else}
            {$additionalCustomPost_grouptitle}
           {/if}
          </td>
         </tr>
{*  Printing First Name and Last Name in two separate rows *} 
         {foreach from=$val item=v key=f}
          <tr>
           <td {$labelStyle}>
            {$f}
           </td>
           <td {$valueStyle}>
            {$v}
           </td>
          </tr>
         {/foreach}
        {/if}
       {/foreach}
      {/foreach}
     {/if}
 

  

The above code creates, again, rows for each person in the family and lists firstName and lastName.  The code additionally moves the value of ticket above the rows where first and last name are printed.