Yii 2: Can I access a variable in a view displayed by another view?

In Yii 2, following the MVC pattern, the controller passes certain variables to the mind. However, sometimes the view makes a different appearance on its own.

For example, in the CRUD views created by default, both create.php and update.php display the _form view:

<?= $this->render('_form', [
    'model' => $model,
]) ?>

Is it possible to use the variable passed by the controller to create.php in _form?

Say the controller does it like this:

return $this->render( 'create', [
    'model' => $model,
    'myVar' => $myValue,
] );

Now I can access $ myVar in the create.php file, but I cannot in _form (which is rendered using create.php. Is there anyway I can access it? Or do I need to explicitly pass it like this, in create.php file):

return $this->render( '_form', [
    'model' => $model,
    'myVar' => $myValue,
] );
+4
source share
3

render - extract() , include . "" .

, render ( ) , , "" . , , .

+3

, .

:

:

return $this->render('view1', ['var' => $value]);

view1:

<?= $this->render('view2', ['var' => $var]) ?>

var $var view2.

+1

I used this to pass a variable from one view to another:

View1:

<?= $this->render('@app/views/layouts/_view2.php', ['hideCarousel' => TRUE]) ?>

View2:

<?php if (!isset($hideCarousel)): ?>
    ...
<?php endif; ?>
-1
source

All Articles