问题
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