Answer the question
In order to leave comments, you need to log in
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,
]);
\Yii::$app->view->renderFile('@app/views/site/pict.php');
$this->render('pict');
echo $this->render('pict', ['post' => $post]);
echo $this->render('report', [
'foo' => 1,
'bar' => 2,
]);
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,
]);
}
}
<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
use yii\widgets\ActiveForm;
use yii\helpers\Url;
?>
<h1><?= Html::encode($post) ?></h1>
<?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]);
?>
Answer the question
In order to leave comments, you need to log in
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
]);
}
https://yiiframework.com.ua/en/doc/guide/2/structu...
section - Passing data between views
This moment is described in the standard documentation.
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.
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 questionAsk a Question
731 491 924 answers to any question