Home > PHP Framework > YII > How to pass variables to template in Yii

How to pass variables to template in Yii

(*-*)浩
Release: 2019-11-07 13:51:19
Original
2861 people have browsed it

How to pass variables to template in Yii

#The easiest way to add dynamic content is to embed PHP statements in the view template file. Any code between the tags will be executed.                                                                                                                                          

<h3><?php echo date("D M j G:i:s T Y"); ?></h3>
Copy after login

In the view file:

$theTime=date("D M j G:is T Y");
$this->render(&#39;helloWorld&#39;,array(&#39;time&#39;=>$theTime));
Copy after login

The view and the controller are very close brothers, so $this in the view file refers to rendering this The view's controller.


Define public properties of a class in the controller instead of local variables. Then access the properties of this class through $this in the view.

<h3><?php echo $time; ?></h3>
Copy after login

In the view file:


class MessageController extends Controller {
    public $time;
    public function actionHelloworld() {
           $this->time = date("D M j G:is T Y");
       $this->render(&#39;helloworld&#39;, array(&#39;time&#39; => $theTime));
    }
Copy after login

The above is the detailed content of How to pass variables to template in Yii. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
yii
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template