V
V
Vladimir2017-02-13 03:39:14
Yii
Vladimir, 2017-02-13 03:39:14

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>

I do like this:
$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>',
    ]);

I tried to write the array of anchors into a variable and pass the linkTemplate as an array, but still nothing ... Tell me
how to get such a result?
data-nav-section="$anchor"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2017-02-13
@ipdesign

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 question

Ask a Question

731 491 924 answers to any question