Answer the question
In order to leave comments, you need to log in
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:
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
)
)
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 .= '])';
}
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' => '',
],
],
]);
Answer the question
In order to leave comments, you need to log in
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( '...' );
}
}
<?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 } ?>
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>';
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question