Inserting html tag on NavBar

Im playing with the default Yii theme. I replace the brandlabel from text to image using:


 'brandLabel' =>  Html::img('@web/images/logo.png',['id' => 'logo']) 

and it worked perfectly. Now I want to put a lock icon next to the Logout word, so I used the html helper again:


$menuItems[] = [

                    'label' => Html::tag('span','',['class' => 'glyphicon glyphicon-lock']).' Logout (' . Yii::$app->user->identity->username . ')',

                    'url' => ['/site/logout'],

                    'linkOptions' => ['data-method' => 'post']

                ]; 



This time, instead of showing the tag, shows the tag as text (at some point its escaped so I see the text <span class="glyphicon glyphicon-lock"></span> instead of the lock icon).

How it needs to be done in this case?

Try using encodeLabels as false


 

 echo Nav::widget([

            	'options' => ['class' => 'navbar-nav navbar-right'],

            	'encodeLabels' => false,

            	'items' => $menuItems

1 Like

The example is using NavBar::begin and NavBar::end instead of Nav::widget. Is a way to use it in that way? the property encodeLabels is not present in NavBar class.

I have try to run with your config. It 's worked fine. I don’t understand your problem


NavBar::begin(['brandLabel' =>  Html::img('@web/images/logo.png',['id' => 'logo'])]);

echo Nav::widget([

    'items' => [

        ['label' => 'Home', 'url' => ['/site/index']],

        ['label' => 'About', 'url' => ['/site/about']],

    ],

]);

NavBar::end();

Try this


<?php

            NavBar::begin([

                'brandLabel' => 'myRide',

                'brandUrl' => Yii::$app->homeUrl,

                'options' => [

                    'class' => 'navbar-inverse navbar-fixed-top',

                ],

            ]);            

            if (Yii::$app->user->isGuest) {

                $menuItems[] = ['label' => Icon::show('home').'Home', 'url' => ['/site/index']];

                $menuItems[] = ['label'=> Icon::show('account').'Account', 'items' => [

                                        ['label' => Icon::show('pencil').'Signup', 'url' => ['/site/signup']],

                                        ['label' => Icon::show('sign-in').'Login', 'url' => ['/site/login']],

                ]];

            } else {

                 $menuItems[] = ['label' => Icon::show('home').'Home', 'url' => ['/site/index']];

                $menuItems[] = ['label'=> Icon::show('account').'Account', 'items' => [

                                        ['label' => Icon::show('cogs').' Profile', 'url' => ['/site/signup']],

                                        ['label' => Icon::show('sign-out').'Logout (' . Yii::$app->user->identity->username . ')',                    'url' => ['/site/logout'], 'linkOptions' => ['data-method' => 'post']],

                ]];           

                

            }

            echo Nav::widget([

                'options' => ['class' => 'navbar-nav navbar-right'],

                'items' => $menuItems,

                'encodeLabels' => false,

            ]);

            NavBar::end();

        ?>

You are misundertood problem. He asked about set Html in NavBar label not about example.

He wanted to add an icon to the nav-bar right?

That code there does that, see attached image.