V
V
Valery Yumshanov2017-05-21 12:56:02
Yii
Valery Yumshanov, 2017-05-21 12:56:02

How to pass parameters/variables from view to view in Yii2?

I figured out how to include from one view to another. Not figured out how to pass parameters/variables from view to view?
The code below doesn't work.

$this->render('@app/views/site/index', [
    'var' => $var,
]);

The documentation did not help, perhaps because there is often a small piece of code taken out of context.
one)
\Yii::$app->view->renderFile('@app/views/site/pict.php');

2) Using methods 1 and 2, it was possible to include files, but without passing variables. As soon as you insert variables as an array into the second parameter, it gives an error . The problem is not that this moment is described in the standard documentation, but that it is not sufficiently covered, i. we just have a small piece of code taken out of context$this->render('pict');
echo $this->render('pict', ['post' => $post]);
echo $this->render('report', [
'foo' => 1,
'bar' => 2,
]);

I replaced the data/variables with the necessary ones, instead of report I put the required view from which I need to take these data/variables.
It turned out: But it doesn't work! We need a code listing - the view from which the data / variables were taken and the view into which they were inserted. Here is my listing: PictController.php controller file (controllers/PictController.php)
echo $this->render('post', ['post' => $post]);
namespace app\controllers;

use Yii;
use yii\web\Controller;
use app\models\UploadForm;
use yii\web\UploadedFile;
use yii\data\Pagination;
use app\models\Pict;
use app\models\PictForm;
use app\models\Country;
use yii\web\CookieCollection;
use yii\web\Session;
use yii\web\NotFoundHttpException;
use yii\base\Action;
use app\components\WorldAction;

class PictController extends Controller
{

public function actionIndex()
    {
$title = "Мой заголовок";
$description = "Описание страницы";
$keywords = "Ключевики";

return $this->render('index', [
    'title' => $title,
    'description' => $description,
    'keywords' => $keywords,

        ]);
    }

public function actionPost()
    {
        $post = "Запись";

        return $this->render('post', [
            'post' => $post,
        ]);
    }

}

View are in the same folder. post.php view file (views/pict/post.php)
<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
use yii\widgets\ActiveForm;
use yii\helpers\Url;
?>

<h1><?= Html::encode($post) ?></h1>

View file index.php (views/pict/index.php)
<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
use yii\widgets\ActiveForm;
use yii\helpers\Url;
?>

<?php 
$this->title=$title;
$this->registerMetaTag(['description' => $description, 'keywords' => $keywords]);
 ?>

<h1><?= Html::encode($this->title) ?></h1>

<?php echo $this->render('post', ['post' => $post]);
 ?>

If the rendering code is removed, both files open normally in the browser.
PHP Notice error text
- yii\base\ErrorException
Undefined variable: post

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Maxim Fedorov, 2017-05-22
@qonand

You are trying to render the post.php file from index.php by passing the $post variable to it, but you do not pass this variable to index.php itself, but you need, for example:

public function actionIndex()
{
    $title = "Мой заголовок";
    $description = "Описание страницы";
    $keywords = "Ключевики";
    $post = "Запись";
    return $this->render('index', [
        'title' => $title,
        'description' => $description,
        'keywords' => $keywords,
        'post' => $post
    ]);
}

A
Access Denied, 2017-05-21
@AccessDenied80

https://yiiframework.com.ua/en/doc/guide/2/structu...
section - Passing data between views
This moment is described in the standard documentation.

M
Maxim Timofeev, 2017-05-21
@webinar

calling from a view
works 100% if there is a pict.php view and if there is a post variable in the view from which you are calling. The error is elsewhere, and if instead of a bunch of nonsense, you still provide the text of the error, then we can obviously help.

V
Valery Yumshanov, 2017-05-21
@scanderberg

The code listing has already been inserted into the question. The problem is that there is such a file, and the variable you need is in it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question