有条件地显示导航小部件的项目 [英] Conditionally show items of Nav widget

查看:19
本文介绍了有条件地显示导航小部件的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为菜单项添加一个条件,这样当用户登录时,他将根据他的 user_type 看到菜单.这是我的代码.

Nav::widget(['编码标签' =>错误的,'选项' =>['类' =>'侧边栏菜单'],'物品' =>[//我想在这里插入条件['标签' =>'<span class="fa fa-fw fa-globe"></span>菜单 1','网址' =>['/menu1'],],['标签' =>'<span class="fa fa-fw fa-list-alt"></span>菜单 2','网址' =>['/menu2'],],]);

有些用户可以访问menu1,而其他用户只能访问menu2.

解决方案

1) 对于单个项目使用 visible 属性(信息可用 这里):

<预><代码>['标签' =>'<span class="fa fa-fw fa-globe"></span>菜单 1','网址' =>['/menu1'],'可见' =>$条件,],

2) 作为替代方案,您可以在呈现小部件之前构建数组,并根据条件有条件地包含/排除某些数组项.

$items = [];如果($条件){$items[] = ...} 别的 {...}回声导航::小部件(['物品' =>$物品,]);

例如查看 高级模板前端布局.

I want to add a condition to menu items so when a user logs in, he will see menu depending on his user_type. Here is my code.

Nav::widget([
    'encodeLabels' => false,
    'options' => ['class' => 'sidebar-menu'],
    'items' => [
    // I want to insert condition here
    [
        'label' => '<span class="fa fa-fw fa-globe"></span> Menu1',
        'url' => ['/menu1'],
    ],
    [
        'label' => '<span class="fa fa-fw fa-list-alt"></span> Menu2',
        'url' => ['/menu2'],
    ],
]);

Some users can access the menu1 and others can access only the menu2.

解决方案

1) For single item use visible property (info is available here):

[
    'label' => '<span class="fa fa-fw fa-globe"></span> Menu1',
    'url' => ['/menu1'],
    'visible' => $condition,
],

2) As an alternative you can build array before rendering the widget and conditionally include / exclude some items of array dependending on conditions.

$items = [];

if ($condition) {
    $items[] = ...
} else {
    ...
}

echo Nav::widget([
    'items' => $items,
]);

See for example how menu items are formed in advanced template frontend layout.

这篇关于有条件地显示导航小部件的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆