where is $content variable declared in Yii?
I am using Yii Framework. In the view, main.ph开发者_Python百科p, 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.
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.
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:
beforeRender
is called.renderPartial
is called on your view file, and its output is stored in$output
.renderFile
is called on the layout file, with a parameter namedcontent
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
Found answer from Yii Documentation / Layouts,
For example, a layout may contain a header and a footer, and embed the view in between, like this:
......header here......
<?php echo $content; ?>
......footer here......
where $content stores the rendering result of the view.
It is indeed all the text in one of the view (in my case index.php). $content basically takes the content of view. It is not declared anywhere and it is be default. As the answer said, you should not use declare/use $content in your code.
I think its being set from the controller which is calling this view.
In the controller look for something like the following
$this->render('main', array('content'=>"something here"));
精彩评论