Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

where is $content variable declared in Yii?

Tags:

php

yii

I am using Yii Framework. In the view, main.php, there is a reference to the $content block

<?php echo $content; ?>

I could not find it anywhere in the model or elsewhere in the demo project. Can someone shed light on this? Or may be this variable is never declared? I have not modified the demo project yet.

like image 791
Hammad Khan Avatar asked Dec 10 '25 14:12

Hammad Khan


2 Answers

The $content value in layout files contains the rendered content of the template specified as the first attribute of the render command. (It's automatically created so I wouldn't use "content" as an additional variable name or it could cause confusion.) The variables that you pass as an additional array argument in the render statement are made available to the template you are calling, not to the layout.

If you have nested layouts, the value of $content cascades from parent to child.

like image 66
ldg Avatar answered Dec 12 '25 02:12

ldg


All your controllers are derived from CController class. CController has a function named render which you call it for rendering your views. It works like this:

  1. beforeRender is called.
  2. renderPartial is called on your view file, and its output is stored in $output.
  3. renderFile is called on the layout file, with a parameter named content like this:

    $this->render(layoutFile, array('content'=>$output));

So the $content is coming from here. You can see the actual code here: Source code, and documentation here: Documentation

like image 43
Abbas Mashayekh Avatar answered Dec 12 '25 03:12

Abbas Mashayekh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!