adding target="_blank" to menu a tags

Hello,

I’m having trouble adding target="_blank" to links in my menus so that users will open a new window when following the link.

I’ve added this as a menu item:




                    [

                        'label' => 'This is the menu label', 

                        'url' => Yii::$app->urlManagerSite->createUrl('some url here'),

                        'template' => '<a href="{url}" target="_blank">{label}</a>',

                    ],



am I setting the menu item label template correctly? For some reason the template isn’t rendered. This is the HTML I get:


<a href="//some url here">This is the menu label</a>



-Charlie

May be you should try like the Nav::widget wants:


[

              'label' => 'This is the menu label',

              'url' => ['some url here'],

              'linkOptions' => ['target'=>'_blank'],

          ],

1 Like

so hrnair is right. Just use linkOptions. ;)

ah - yes, a bit sheepish over here - I was looking at the documentation for the yii\widgets\Menu, yet I was actually using yii\bootstrap\Nav.

linkOptions it is.

thanks,

-Charlie