开发者

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:

  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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜