A
A
ANTVirGEO2015-11-30 12:41:30
Yii
ANTVirGEO, 2015-11-30 12:41:30

How to convert string to html in YII2 when passing widget code to view?

Good afternoon!
There is incoming data in the form of an array tree:

spoiler

Array
(
    [11] => Array
        (
            [id] => 11
            [name] => one
            [description] => first
            [child] => Array
                (
                    [33] => Array
                        (
                            [id] => 33
                            [name] => three
                            [description] => third
                        )
                )
        )
    [22] => Array
        (
            [id] => 22
            [name] => two
            [description] => second
            [child] => Array
                (
                    [11] => Array
                        (
                            [id] => 11
                            [name] => one
                            [description] => first
                            [child] => Array
                                (
                                    [33] => Array
                                        (
                                            [id] => 33
                                            [name] => three
                                            [description] => third
                                        )
                                )
                        )
                    [33] => Array
                        (
                            [id] => 33
                            [name] => three
                            [description] => third
                        )
                )
        )
    [33] => Array
        (
            [id] => 33
            [name] => three
            [description] => third
        )
)


They are processed in the model by this code:
spoiler

private function getChildsForView($child){
        $this->list .= 'Collapse::widget([\'items\' => [';
        foreach($child as $k => $v){
            $this->list .= '[
                    \'label\' => \'' . $v['name'] . ' - ' . $v['description'] . '\'';
            $this->list .= ',\'content\' =>  ';
            if(isset($v['child'])) {
                $this->list .= self::getChildsForView($v['child']);
            } else $this->list .= '\'\',]';
            $this->list .= ',],';
        }
        $this->list .= '])';
    }


I want to wrap the data in a Collapse bootstrap widget , resulting in the following code:
spoiler

Collapse::widget(
   [
      'items' => [
            [ 
                  'label' => 'one - first',
                  'content' => Collapse::widget(
                        [
                              'items' => [
                                    [ 
                                          'label' => 'three - third',
                                          'content' => '',
                                    ],
                              ],
                        ]),
            ],
            [ 
                  'label' => 'two - second',
                  'content' => Collapse::widget(
                        [
                              'items' => [
                                    [ 
                                          'label' => 'one - first',
                                          'content' => Collapse::widget(
                                                [
                                                      'items' => [
                                                            [ 
                                                                  'label' => 'three - third',
                                                                  'content' => '',
                                                            ],
                                                      ],
                                                ]),
                                    ],
                                    [ 
                                          'label' => 'three - third',
                                          'content' => '',
                                    ],
                              ],
                  ]),
            ],
            [ 
                  'label' => 'three - third',
                  'content' => '',
            ],
      ],
]);


However, when it is passed to the view, it treats it directly as text and doesn't display the widget as such at all, just displaying that text. However, if you just take the text of the variable and display it through echo, then everything is normally perceived: 5d49a33a7252462898f4f1ef853c1b0b.png
Question - how to force YII2 to interpret my variable with code NOT as text, but as code?
PS: html_entity_decode, htmlspecialchars_decode don't help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly Khomenko, 2015-11-30
@ANTVirGEO

Oh Gods! And then how to support, with a fortune teller?
Yes, you can take it all into a separate specially created widget. Place the logic in the methods of the widget class and generate the output directly in the widget's view. It's much simpler and clearer... Let's
use the created widget in the required view:
The widget contains all the necessary methods:

class MyWidget extends \yii\bootstrap\Widget
{
    public $list = [];

    public function someMethod () {}

    public function run () {
        return $this->renderFile( '...' );
    }
}

And the actual widget view:
<?php
use path\to\widget\MyWidget;
use yii\web\View;

/**
 * @var View     $this
 * @var MyWidget $widget
 */

$widget = $this->context;
?>

<!-- Тут мы используя циклы и методы виджета формируем отображение -->

<?php foreach( $widget->... as $value ) { ?>

    <!-- Делаем что нужно и как нужно -->

<?php } ?>

A
ANTVirGEO, 2015-12-03
@ANTVirGEO

spoiler
private function getChildsForView($child){
        $this->c++;
        foreach($child as $k => $v){
            $this->list .= '<div class="panel panel-default">';
                $this->list .= '<div class="panel-heading tick" role="tab" id="heading' . $this->c . '" style="cursor: pointer" data-toggle="collapse" data-parent="#accordion" href="#collapse' . $this->c . '" aria-controls="collapse' . $this->c . '">';
                    $this->list .= '<h4 class="panel-title">';
                        $this->list .= '<img align="middle" id="greentick" src="../images/greenTick.png" class="greenTick" style="display: none">';
                        $this->list .= $v['name'];
                        if(isset($v['child']) && count($v['child']) > 0) $this->list .= ' ' . Html::badge(count($v['child']));
                    $this->list .= '</h4>';
                $this->list .= '</div>';
                if(isset($v['child']) && count($v['child']) > 0){
                    $this->list .= '<div id="collapse' . $this->c . '" class="panel-collapse collapse out" role="tabpanel" aria-labelledby="heading' . $this->c . '">';
                        $this->list .= '<div>';
                            $this->list .= '<ul>';
                                foreach($v['child'] as $kk => $vv){
                                    $this->list .= '<li>';
                                    $this->list .= self::getChildsForView($v['child']);
                                    $this->list .= '</li>';
                                }
                            $this->list .= '</ul>';
                        $this->list .= '</div>';
                    $this->list .= '</div>';
                }
            $this->list .= '</div>';
        }
    }

This is how the final generator looks like (to the comment to the first answer)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question