2016-12-08 10:58:50 3348次浏览 3条回答 1 悬赏 10 金钱

问题:

$arr = [
    Html::img( '/abc/image1.jpg', [ 'alt' => '头像' ] ), 
    Html::img( '/abc/image2.jpg', [ 'alt' => '头像' ] ), 
];
Html::ul( $arr );

结果:

<li>
    &lt;img src=&quot;/abc/image1.png&quot; alt=&quot;头像&quot; style=&quot;width:50px;height:50px&quot;&gt;
</li>
<li>
    &lt;img src=&quot;/abc/image2.png&quot; alt=&quot;头像&quot; style=&quot;width:50px;height:50px&quot;&gt;
</li>

得到的是转义符而不是html标签了...
哪位大哥求解决下。。。

最佳答案

  • 师阳 发布于 2016-12-08 12:25 举报

    看yii2源码,这里不想被转义就应该用
    Html::ul($arr, ['encode' => false]);

        /**
         * Generates an unordered list.
         * @param array|\Traversable $items the items for generating the list. Each item generates a single list item.
         * Note that items will be automatically HTML encoded if `$options['encode']` is not set or true.
         * @param array $options options (name => config) for the radio button list. The following options are supported:
         *
         * - encode: boolean, whether to HTML-encode the items. Defaults to true.
         *   This option is ignored if the `item` option is specified.
         * - separator: string, the HTML code that separates items. Defaults to a simple newline (`"\n"`).
         *   This option is available since version 2.0.7.
         * - itemOptions: array, the HTML attributes for the `li` tags. This option is ignored if the `item` option is specified.
         * - item: callable, a callback that is used to generate each individual list item.
         *   The signature of this callback must be:
         *
         *   
         *   function ($item, $index)
         *   
         *
         *   where $index is the array key corresponding to `$item` in `$items`. The callback should return
         *   the whole list item tag.
         *
         * See [[renderTagAttributes()]] for details on how attributes are being rendered.
         *
         * @return string the generated unordered list. An empty list tag will be returned if `$items` is empty.
         */
        public static function ul($items, $options = [])
        {
            $tag = ArrayHelper::remove($options, 'tag', 'ul');
            $encode = ArrayHelper::remove($options, 'encode', true);
            $formatter = ArrayHelper::remove($options, 'item');
            $separator = ArrayHelper::remove($options, 'separator', "\n");
            $itemOptions = ArrayHelper::remove($options, 'itemOptions', []);
    
    
    
    1 条回复
    回复于 2016-12-08 13:22 回复

    小弱鸡新手十分感谢,谢谢大哥!

    觉得很赞
您需要登录后才可以回答。登录 | 立即注册
xiam
试用期

xiam

注册时间:2016-05-01
最后登录:2017-01-04
在线时长:2小时27分
  • 粉丝0
  • 金钱10
  • 威望0
  • 积分30

热门问题