Roby Sottini
Roby Sottini

Reputation: 2265

Yii2: How to apply ntext format with html format in a GridView?

I have the column called query and the column called description from an index.php file generated with Gii Generator for Yii2. Both belong to the GridView widget.

I want to use the ntext format provided by Yii2 for the query column because I need the breaks.

ntext: formats the value as an HTML-encoded plain text with newlines converted into breaks.

'columns' => [
    'description:ntext',
    [
        'attribute' => 'query',
        'format' => 'html',
        'value' => function($model) {
            return "<span style='font-family: Dejavu Sans, monospace'>" . $model->query . '</span>';
        }
    ],
]

Here I can't show the breaks: Query field

Upvotes: 1

Views: 1659

Answers (1)

rob006
rob006

Reputation: 22164

You should use asNtext() directly:

'columns' => [
    'description:ntext',
    [
        'attribute' => 'query',
        'format' => 'html',
        'value' => function($model) {
            return "<span style='font-family: Dejavu Sans, monospace'>" 
                . Yii::$app->formatter->asNtext($model->query) . '</span>';
        }
    ],
]

Upvotes: 5

Related Questions