Yii zii.widgets.CDetailView - Output an attribute

2019-04-15 03:43发布

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条回答
Root(大扎)
2楼-- · 2019-04-15 04:31

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',
查看更多
登录 后发表回答