Answer the question
In order to leave comments, you need to log in
How to dynamically generate linkTemplate for Menu::widget?
Hello!
I need to bring the list of links in the menu to this form:
<li><a href="#" data-nav-section="testimonials"><span>Отзывы</span></a></li>
<li><a href="#" data-nav-section="services"><span>Услуги</span></a></li>
<li><a href="#" data-nav-section="about"><span>О компании</span></a></li>
$menuItems = [
['label' => 'Отзывы', 'url' => ['/'], 'tag' => 'testimonials'],
['label' => 'Услуги', 'url' => ['/'], 'tag' => 'services'],
['label' => 'О компании', 'url' => ['/'], 'tag' => 'about'],
];
echo Menu::widget([
'options' => [
'class' => 'nav navbar-nav navbar-right text-center'
],
'items' => $menuItems,
'linkTemplate' => '<a href="{url}" data-nav-section=""><span>{label}</span></a>',
]);
data-nav-section="$anchor"
Answer the question
In order to leave comments, you need to log in
in your case, I see the option of using only template for each menu item, for example:
$menuItems = [
['label' => 'Отзывы', 'url' => ['/'], 'template' => '<a href="{url}" data-nav-section="testimonials"><span>{label}</span></a>'],
['label' => 'Услуги', 'url' => ['/'], 'template' => '<a href="{url}" data-nav-section="services"><span>{label}</span></a>'],
['label' => 'О компании', 'url' => ['/'], 'template' => '<a href="{url}" data-nav-section="about"><span>{label}</span></a>'],
];
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question