Answer the question
In order to leave comments, you need to log in
How to insert the active class into the site menu during foreach?
Good afternoon. I have been practicing a little in creating a site, and there was a small problem with the menu of this site.
When a button is clicked, the active class should be added, which highlights the pressed button.
But I want to completely add the menu to the database, for the convenience of changing or adding new items. And here the problem began, I get data from the database,
<?php if(!empty($navLinks)):?>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<?php foreach ($navLinks as $navLink):?>
<li><a href="<?= $navLink->href?>" class="scroll-to"><?= $navLink->name?></a></li>
<?php endforeach;?>
</ul>
</div>
<?php endif;?>
Answer the question
In order to leave comments, you need to log in
Good evening.
Use the widgets provided by yii2, everything will work.
For example yii\widgets\Menu;
You get all the necessary menu items from the database, form them into an array and "feed" them into the widget.
Menu::widget([
'items' => $items
])
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question