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
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.
It's actually very simple:
{assign 'myArray' ['cat', 'dog', 'rabbit']}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With