CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • CiviCRM email template Use of array in Smarty
Pages: [1]

Author Topic: CiviCRM email template Use of array in Smarty  (Read 1857 times)

pjindent

  • I post occasionally
  • **
  • Posts: 34
  • Karma: 2
CiviCRM email template Use of array in Smarty
March 03, 2011, 06:48:58 pm
I am trying to put code into an email template of CiviCRM. I want to use an array.  All attempts I have made have failed.

attempt 1:
{assign var=dollaramounts value=[]}  /* declaration of array
{$dollaramounts[]=20} /* assignment into element 0 of array
/* I get a syntax error on the second line

Then I tried:
{assign var=dollaramounts value=[20]}  /* declaration of array and putting value into element 0
{$dollaramounts[]=20} /* assignment in element 1 of array
/* I get a syntax error on the second line

I have tried various others methods, but no luck.

Appreciate any help.

pjindent

  • I post occasionally
  • **
  • Posts: 34
  • Karma: 2
Re: CiviCRM email template Use of array in Smarty
March 03, 2011, 06:56:29 pm
We are using CiviCRM 3.1.5.  Does the version of Smarty used in CiviCRM support arrays?

Rajan Mayekar

  • I post frequently
  • ***
  • Posts: 177
  • Karma: 20
    • Rajan's Blogs
Re: CiviCRM email template Use of array in Smarty
March 04, 2011, 02:05:01 am
Some days before I was trying do something like above but at last came with {php} {/php}  :(

So within tpl, you can use as:
Code: [Select]

{php}
$dollaramounts = array();
$dollaramounts[0] = 20;
$this->assign( 'dollaramounts', $dollaramounts );
{/php}

Here is the value of dollaramounts at index 0 : {$dollaramounts.0}

There may be the another better solution to do this, but for now above solution will definitely work  :)

Rajan

pjindent

  • I post occasionally
  • **
  • Posts: 34
  • Karma: 2
Re: CiviCRM email template Use of array in Smarty
March 05, 2011, 06:31:22 am
thanks for the response.  I tried the following and did not work.  How do you mix and match new smarty variables and existing variables in the email template.

Code: [Select]
 
{php}
  $dollaramount = array();
          foreach ($amount as $level => $amnt) {  /* $amount is an existing variable in the email template. 
                                               /*  In the email template $amount is used like this:  {foreach from=$amount item=amnt key=level}
               $dollaramount[] = $amnt.amount;
       }
$this->assign('dollaramount' , $dollaramount);
{/php}
« Last Edit: March 05, 2011, 06:33:08 am by pjindent »

Rajan Mayekar

  • I post frequently
  • ***
  • Posts: 177
  • Karma: 20
    • Rajan's Blogs
Re: CiviCRM email template Use of array in Smarty
March 06, 2011, 08:42:48 pm
Something looks wrong in ur code, instead  $amnt.amount u should use $amnt['amount'] (since it is in php ), make sure 'amount' key is actually present in $amnt.
Code: [Select]
foreach ($amount as $level => $amnt) {
$dollaramount[] = $amnt['amount'];  // assuming 'amount' key specify amount in $amnt
}

Quote
How do you mix and match new smarty variables and existing variables in the email template
You can directly use any smarty variable as a php variable within {php} {/php}

Use can {debug} within smarty, to check smarty variables.

BTW
assigning variable to the array in smarty like
{$dollaramounts[]=20} is a part of smarty v3 but CiviCRM using smarty v2.6.26.

Rajan

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • CiviCRM email template Use of array in Smarty

This forum was archived on 2017-11-26.