tags:

views:

48

answers:

2

Hi,

Is it possible to construct an array in Smarty, for example I have tried

{def $totalitems[0]=3}

But that doesn't seem to work. Is it possible in Smarty?

Thanks.

+1  A: 

I'm not sure why you would want to do this. The idea behind a template system is that you are separating the logic from the display. You need to build the array in PHP and then pass it into your smarty template using php like this:

$totalitems[0]=3;
$smarty->assign("totalitems",$totalitems);

Then you can access totalitems from within your template in the normal fashion.

Rook
+1, and fixed the spelling of 'assign'.
karim79
A: 

In Smarty3 Beta you can do the following:

Examples: {$foo['bar']=1} {$foo['bar']['blar']=1}

Just look at the README: http://smarty-php.googlecode.com/svn/branches/Smarty3Dev/distribution/README

I am not sure if you can do it in Smarty2. I have tried a few things on my version of Smarty2, but it doesn't work. You might need to upgrade to Smarty3.

However, I would recommend against doing logic operations in the template, if it can be helped.

rlorenzo