pishva
(Aminmazrouei)
December 1, 2014, 1:30pm
1
hi. im trying to populate yii2 menu widget items by using a foreach . but it seems its not possible . and it shows either the variables as string or occour an error.
so in below code how can i define ‘label’ value and ‘<span></span>’ and ‘url’ value by helping a foreach loop
<?= \yii\widgets\Menu::widget([
'options' => ['class' => 'menu', 'rel' => 'sam1'],
'items' => [
['label' => 'All', 'url' => ['product/index', 'category' => 'All'],
'template' => '<a href="{url}">{label}<span>(55)</span></a>'],
]]
) ?>
pishva
(Aminmazrouei)
December 1, 2014, 5:34pm
2
OK. it’s so easy …
just use a parameter and pass it to ‘item’
foreach ($category as $category_item) {
$items[$array_start++] = array('label' => $category_item['name'],
'url' => ['product/index', 'cat_id' => $category_item['id'], 'cat_name' => $category_item['name']],
'template' => '<a href="{url}">{label}<span>(' . $category_item['count'] . ')</span></a>'
);
$totalcount += $category_item['count'];
}
in widget :
<?= \yii\widgets\Menu::widget([
'options' => ['class' => 'menu', 'rel' => 'sam1'],
'items' => $items,
]
) ?>