Answer the question
In order to leave comments, you need to log in
How to pass variable from view to layout in twig?
Greetings. I can’t figure out how to pass any variables from the view to the layout, for example, I want to change the y classes on each page. By the way, the blocks also do not change the default value from the layout.
I use yii2-twig, I wrote $layout in the application config
Answer the question
In order to leave comments, you need to log in
This is a bit contrary to the concept of Yii2 that she gives. a ready-made view, completely assembled from filters and widgets, goes into the view. Most of the code is already encapsulated and left FROM the server, and either forms or a controller with a request for some action are used for transmission. To change classes, it is better to come up with something from the controller, and the view should only deal with displaying content.
But! I think you can try this, it once worked in Yii 1.x
in the form
/*
*Доп. параметр, причем он в layout-е
*будет доступен только в секции body,
*в секции head нет!
*/
$this->params['myparam'] = $myparam;
<?php $this->beginBody() ?>
<!-- Причем это доступно только в секции body -->
<?= $this->params['myparam']?>
<?= $content ?>
<?php $this->endBody() ?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question