问题
How can i generate dynamics variable names in smarty?
I trying using {$item.schedule.d{$i}}
to generate something like this {$item.schedule.d1}, {$item.schedule.d2},
...
回答1:
You can do this that way:
In PHP:
$items = array();
$items['schedule']['d1'] = 'D1';
$items['schedule']['d2'] = 'D2';
$items['schedule']['d3'] = 'D3';
$smarty->assign('items',$items);
$smarty->display('index.tpl');
In index.tpl :
{$data = [1,2,3]}
{foreach $data as $value}
{$items.schedule["d{$value}"]}<br />
{/foreach}
So in your case you should use {$item.schedule["d{$i}"]}
syntax
来源:https://stackoverflow.com/questions/23933787/dynamics-variables-in-smarty-in-loop