L
L
Lev K2016-05-23 16:15:33
Yii
Lev K, 2016-05-23 16:15:33

How to generate html code in yii2 model?

There was a need to create html code in a model or controller, then pass it to the view and draw it there. That's how I imagine it to be, but it doesn't work. Is there such a possibility at all?
Model

public function getImage()
    {
        $str = '';        
        foreach ($this->file as $img) { // file - геттер связи с другой таблицей
            $str = $str + Html::img(Url::to([$img->Image]), ['width'=>'100px']) + ' ';
// Image - атрибут, в котором храниться путь к файлу 
        }
        return $str;
    }

Performance
<?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            // other att
            [
                'label' => 'Картинки',
                'value' => $model->image, 
            ],
        ],
    ]); ?>

I think that it will be possible to do the same with the controller (generate code there and send it to the view)
PS For such manipulations, you need to add 'format' => 'html' before value

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lev K, 2016-05-23
@Leffken

Works like this

Модель
public function getImage()
    {
        $str = '';
        foreach ($this->file as $img) {
            $str = $str . Html::img(Url::to([$img->Image]), ['width'=>'100px']) . ' ';
        }
        return $str;
    }

Performance
<?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            // other attr
            [
                'label' => 'Image',
                'format' => 'html',
                'value' => $model->image, 
            ],
        ],
    ]); ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question