Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add raw HTML in Yii CMenu label

Tags:

html

yii

cmenu

I create a menu like a Twitter Bootstrap navbar with CMenu widget:

<?php 
    $this->widget( 'zii.widgets.CMenu', array(
    'items' => array(
        array(
            'label' => 'Home', 
            'url' => array( '/site/index' ), 
        ),
        array( 
            'label' => 'Dropdown <b class="caret"></b>', 
            'url' => '#',
            'submenuOptions' => array( 'class' => 'dropdown-menu' ),
            'items' => array( 
                array( 
                    'label' => 'Submenu Item 1', 
                    'url' => array( '/user/create' ), 
                ),
                array( 
                    'label' => 'Submenu Item 1', 
                    'url' => array( '/user/list' ), 
                ),
            ),
            'itemOptions' => array( 'class' => 'dropdown' ),
            'linkOptions' => array( 'class' => 'dropdown-toggle', 'data-toggle' => 'dropdown' ),
        ),
        'htmlOptions' => array( 'class' => 'nav' ),
    )); ?>

This code generate menu with 2 items in it and 1 submenu for second menu item. Fine. But only thing, which not worked is 'label' => 'Dropdown <b class="caret"></b>', in 9th line. It rendered as Dropdown &lt;b class=&quot;caret&quot;&gt;&lt;/b&gt; on page. So I see caption 'Dropdown <b class="caret"></b>' instead of Dropdown ▼.

How I can change code to show unescaped HTML in menu label?

Thanks for your attention.

like image 289
doktorgradus Avatar asked Mar 04 '13 15:03

doktorgradus


2 Answers

You have to set encodeLabel attribute of CMenu to false

<?php
$this->widget('zii.widgets.CMenu', array(
    'encodeLabel' => false,
    'htmlOptions' => array('class' => 'nav'),
    'items' => array(
        array(
            'label' => 'Home',
            'url' => array('/site/index'),
        ),
        array(
            'label' => 'Dropdown <b class="caret"></b>',
            'url' => '#',
            'submenuOptions' => array('class' => 'dropdown-menu'),
            'items' => array(
                array(
                    'label' => 'Submenu Item 1',
                    'url' => array('/user/create'),
                ),
                array(
                    'label' => 'Submenu Item 1',
                    'url' => array('/user/list'),
                ),
            ),
            'itemOptions' => array('class' => 'dropdown'),
            'linkOptions' => array('class' => 'dropdown-toggle', 'data-toggle' => 'dropdown'),
        ),
    ),
));
?>
like image 116
dInGd0nG Avatar answered Nov 16 '22 17:11

dInGd0nG


My solution is creating CMenu extension:

layout/main.php

'submenuOptions'=>array('class'=>'dropdown-menu'),
'itemOptions'=>array('class'=>'dropdown'),
'linkOptions'=>array('class'=>'dropdown-toggle', 'data-toggle'=>'dropdown'),
// Dropdown arrow toggle
'dropdownArrow'=>true,

ext/widgets/BootstrapCMenu.php

class BootstrapCMenu extends CMenu {

protected function renderMenuItem($item)
{
    if(isset($item['url']))
    {
        $item['label'] .= ($item['dropdownArrow']) ? ' <b class="caret"></b>' : '';
        $label=$this->linkLabelWrapper===null ? $item['label'] : CHtml::tag($this->linkLabelWrapper, $this->linkLabelWrapperHtmlOptions, $item['label']);
        return CHtml::link($label,$item['url'],isset($item['linkOptions']) ? $item['linkOptions'] : array());
    }
    else
        return CHtml::tag('span',isset($item['linkOptions']) ? $item['linkOptions'] : array(), $item['label']);
}

}
like image 1
Andrey S. Rabchevsky Avatar answered Nov 16 '22 17:11

Andrey S. Rabchevsky