2013-03-04 17 views
10

tôi có thể tạo một menu giống như một thanh điều hướng với CMenu widget Twitter Bootstrap:Làm thế nào để thêm HTML thô trong Yii CMenu nhãn

<?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'), 
    )); ?> 

Mã này tạo menu với 2 mặt hàng trong đó và 1 menu con cho mục trình đơn thứ hai. Khỏe. Nhưng điều duy nhất, mà không làm việc là 'label' => 'Dropdown <b class="caret"></b>', trong dòng thứ 9. Nó được hiển thị dưới dạng Dropdown &lt;b class=&quot;caret&quot;&gt;&lt;/b&gt; trên trang. Vì vậy, tôi thấy chú thích 'Dropdown <b class="caret"></b>' thay vì Dropdown ▼.

Làm cách nào tôi có thể thay đổi mã để hiển thị HTML không thoát trong nhãn menu?

Cảm ơn sự quan tâm của bạn.

+0

http://www.cniska.net/yii-bootstrap/#tbNavbar – hungneox

Trả lời

22

Bạn phải thiết lập thuộc tính của encodeLabelCMenu để 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'), 
     ), 
    ), 
)); 
?> 
+0

Nó hoạt động. Cảm ơn bạn! – doktorgradus

1

Giải pháp của tôi là tạo ra CMenu mở rộng:

bố trí/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']); 
} 

} 
0

Trong Yii 2.0, để thêm glyphicon trong menu thanh điều hướng, bạn có thể thực hiện theo thông tin bên dưới.

Chỉnh sửa trong nhà cung cấp \ yiisoft \ yii2-bootstrap \ Nav.php dưới renderItem chức năng đoạn mã sau:

 if(isset($item['icons'])) 
     $label=Html::tag('span', '', ['class' => 'glyphicon glyphicon-'.$item['icons']]).$label; 

Bây giờ, bạn có thể trực tiếp sử dụng bất kỳ biểu tượng từ mã của bạn với icons tùy chọn như

<?php 
$this->widget('zii.widgets.CMenu', array(
'items' => array(
    array(
     'label' => 'Home', 
     'url' => array('/site/index'), 
     'icons'=> 'home', 
    ), 
    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'), 
)); ?> 

Bạn có thể thực hiện các thay đổi tương ứng ngay cả trong các phiên bản cũ hơn của yii.

Các vấn đề liên quan