Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an array in smarty template? [duplicate]

Tags:

php

smarty

I need to create a new array from other one dimensional array in smarty template. So, what are the best possibilities to create an array in template file.

Thanks, Sachin

like image 954
sachin sawant Avatar asked Jan 19 '12 15:01

sachin sawant


3 Answers

Smarty3 allows you to {$var = ['foo' => 'bar', 'sub' => [1, 2, 3]]} and {$var.foo = 'other'}

if those options don't suffice, write a plugin function.

like image 191
rodneyrehm Avatar answered Nov 20 '22 04:11

rodneyrehm


It's actually very simple:

{assign 'myArray' ['cat', 'dog', 'rabbit']}
like image 14
Eugene Kuzmenko Avatar answered Nov 20 '22 03:11

Eugene Kuzmenko


In the past, I have used two approaches - an evil and a dirty one - to quickly assign an array inside a tpl:

{* Am I evil? *}
{php}
    $array = array("cat", "dog", "rabbit");
    $this->assign("myArray", $array);
{/php}

{* Am I dirty? *}
{assign var='myArray' value=','|explode:"cat,dog,rabbit"}

Both result in your array available inside the template to build a simple loop. Anyway I always ended up changing my code this way, so I did not need this stuff at all.

like image 13
DerVO Avatar answered Nov 20 '22 03:11

DerVO