Question

i want to setup a badge inside my navigation menu with bootstrap and yii to show how many alerts an user has.

    <?php $this->widget('bootstrap.widgets.TbNavbar',array(
        'items'=>array(
            array(
                'class'=>'bootstrap.widgets.TbMenu',
                'items'=>
array('label'=>'Alerts', 'url'=>array('/user/alerts')),
array(array('label'=>'Login', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest),
                    array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest)
                ),
            ),
        ),
    )); ?>

can i put a text and a badge inside it? Like: "3 alerts".. where '3' is my badge? and alerts will be a link to alerts page?

$this->widget('bootstrap.widgets.TbBadge', array(
            'type'=>'inverse',
            'label'=>'2',
        )); 
Was it helpful?

Solution

Third parameter of CBaseController::widget determines if widget content should be captured into variable or directly outputed. By default it outputs content. http://www.yiiframework.com/doc/api/1.1/CBaseController#widget-detail

Also, CMenu class has encodeLabel property http://www.yiiframework.com/doc/api/1.1/CMenu#encodeLabel-detail. Setting it to true will output label as is(without being HTML-encoded)

Try this:

$badge=$this->widget('bootstrap.widgets.TbBadge', array(
    'type'=>'inverse',
    'label'=>'2',
), true);

$this->widget('bootstrap.widgets.TbNavbar',array(
    'items'=>array(
        array(
            'class'=>'bootstrap.widgets.TbMenu',
            'encodeLabel'=>false,
            'items'=>
                array(
                    array('label'=>$badge.' Alerts', 'url'=>array('/user/alerts')),
                    array('label'=>'Login', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest),
                    array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest)
                ),
            ),
    ),
));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top