我基本上没有试过,因为我还没有在我查了文档发现任何东西。Yii 2 - 如何将变量传递给渲染器的主视图文件?
我基本上要传递一个变量,即。 $balance
所以它会在layouts/main.php
文件的范围可供选择:
我在哪里可以这样说:
$balance = MyClass::myMethod();
return $this->render('main', [
'balance' => $balance,
]);
谢谢。希望任何人都明白。
我基本上没有试过,因为我还没有在我查了文档发现任何东西。Yii 2 - 如何将变量传递给渲染器的主视图文件?
我基本上要传递一个变量,即。 $balance
所以它会在layouts/main.php
文件的范围可供选择:
我在哪里可以这样说:
$balance = MyClass::myMethod();
return $this->render('main', [
'balance' => $balance,
]);
谢谢。希望任何人都明白。
// layouts/main.php
var_dump($this->params);
// action method
$this->view->params['balance'] = $balance;
而且看看http://www.yiiframework.com/doc-2.0/guide-structure-views.html#using-blocks
您可以发送变量首先查看文件一样,
$balance = MyClass::myMethod();
return $this->render('myview', [
'balance' => $balance,
]);
所以,你可以在views/controller_name/myview.php
有创建文件myview.php可以直接$balance
访问变量。现在,你可以通过这个变量myview.php
使用params
到main.php(如果它是你的布局文件)。
$this->params['balance'] = $balance;
并通过$this->params['balance']
访问main.php
中的变量。