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

How to pass variables to template in Yii

(*-*)浩
(*-*)浩Original
2019-11-07 13:51:192778browse

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 815b22acdbf9dab14aa760b8cd72f36d tags will be executed.                                                                                                                                          

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

In the view file:

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

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>

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));
    }

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!

Statement:
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
Previous article:How about yiiNext article:How about yii