plosww1
plosww1

Reputation: 333

Constructing arrays in Smarty PHP

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.

Upvotes: 1

Views: 158

Answers (2)

rlorenzo
rlorenzo

Reputation: 1368

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.

Upvotes: 1

rook
rook

Reputation: 67039

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.

Upvotes: 1

Related Questions