【问题标题】:Yii zii.widgets.CDetailView - Output an attribute as HTML code formatYii zii.widgets.CDetailView - 将属性输出为 HTML 代码格式
【发布时间】:2013-09-01 23:27:55
【问题描述】:

我希望在 CDetailView 中将输出属性 description 作为 HTML 代码。

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

【问题讨论】:

  • 所以,我要试试这个 CHtml::decode($model->description),但这行不通

标签: php html yii widget format


【解决方案1】:

您将需要使用:html 格式:

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

其他格式见CFormatter

您甚至可以扩展 CFormatter,并创建自己的格式。

<?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;
    }
}

如果您扩展 CFormatter,请更新项目的 ma​​in.php 以指向新文件:

// application components
'components' => array(

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

    ...
),

示例用法:

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

【讨论】:

  • 或者你可以这样做array('name' =&gt; 'visible', 'type' =&gt; 'html', 'value' =&gt; ($model-&gt;visible ? 'Yes' : '&lt;span style="color: red"&gt;No&lt;/span&gt;')),
猜你喜欢
  • 1970-01-01
  • 2011-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多