Yii zii.widgets.CDetailView - Output an attribute as HTML code format

你说的曾经没有我的故事 提交于 2019-12-06 12:47:29

问题


I want output attribute description as HTML code in CDetailView.

<?php $this->widget('zii.widgets.CDetailView', array(
    'data'=>$model,
    'attributes'=>array(
        'id',
        'title',
        'description' => array(
            'name' => 'description',
            'value' => html_entity_decode(CHtml::decode($model->description)),
        ),
        'price',
        'date',
    ),
));?>

回答1:


You will want to use the :html format:

'attributes'=>array(
        'id',
        'title',
        'description:html',
        'price',
        'date',
    ),

For other formats, see CFormatter.

You can even extend CFormatter, and create your own formats.

<?php
class CustomFormatter extends CFormatter {

    public function formatLink($value) {
        return '<a href="'.$value.'">'.$value.'</a>';
    }

    public function formatBold($value) {
        return '<b>'.$value.'</b>';
    }

    public function formatArray($value) {
        return (is_array($value)) ?
            implode(', ', $value) : $value;
    }
}

If you extend the CFormatter, update your project's main.php to point to the new file:

// application components
'components' => array(

    'format' => array(
        'class' => 'application.extensions.CustomFormatter',
    ),

    ...
),

Example Usage:

    'title:bold',
    'website:link',
    'tags:array',


来源:https://stackoverflow.com/questions/18563909/yii-zii-widgets-cdetailview-output-an-attribute-as-html-code-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!