Yii/Giix-标准2列布局
下面的代码显示了Yii中的两列布局。$content变量包含一个搜索表单和一个gridview表单。 我试图让gridview以这种两列网格格式显示在高级搜索部分的右侧。这里有点像大脑放屁,在标准的Giix结构中,变量$content在哪里?我在basemodel或controller中没有看到它 提前谢谢Yii/Giix-标准2列布局,yii,yii-extensions,Yii,Yii Extensions,下面的代码显示了Yii中的两列布局。$content变量包含一个搜索表单和一个gridview表单。 我试图让gridview以这种两列网格格式显示在高级搜索部分的右侧。这里有点像大脑放屁,在标准的Giix结构中,变量$content在哪里?我在basemodel或controller中没有看到它 提前谢谢 <?php /* @var $this Controller */ ?> <?php $this->beginContent('//layouts/main'); ?
<?php /* @var $this Controller */ ?>
<?php $this->beginContent('//layouts/main'); ?>
<div class="span-24">
<div id="content">
<?php echo $content; ?>
</div><!-- content -->
</div>
<div class="span-5 last">
<div id="sidebar">
<?php
$this->beginWidget('zii.widgets.CPortlet', array(
'title'=>'Operations',
));
$this->widget('zii.widgets.CMenu', array(
'items'=>$this->menu,
'htmlOptions'=>array('class'=>'operations'),
));
$this->endWidget();
?>
</div><!-- sidebar -->
</div>
<?php $this->endContent(); ?>
在特定视图中使用网格布局。应该是这样的
<div class='span-10'>
//search form
</div>
<div class='span-9'>
//grid
</div>
//搜索表
//网格
$content在其操作结束时,当控制器调用$this->render()
时,将为其提供内容
public function actionIndex() {
// renders the view file 'protected/views/site/index.php'
// using the default layout 'protected/views/layouts/main.php'
[some code...]
$this->render('index');
}
所涉及的过程有点模糊,但通过设置断点并查看调试器中的堆栈,您可以轻松地跟踪它
您还可以阅读以下代码:
render()是CController类的一种方法:
public function render($view, $data = null, $return = false) {
if ($this->beforeRender($view)) {
$output = $this->renderPartial($view, $data, true); // (1)
if (($layoutFile = $this->getLayoutFile($this->layout)) !== false)
$output = $this->renderFile($layoutFile, array('content' => $output), true); // (2)
[snip...]
}
}
(1)如果渲染前没有发生错误,则填充视图并将其HTML代码分配给$output:$output=$this->renderPartial($view,$data,true)代码>
(2)然后,除非您在操作中声明视图不能通过冷却$this->setLayout(false)
由布局装饰,否则将应用,并在布局中设置内部视图:
$output = $this->renderFile($layoutFile, array('content' => $output), true)
这里,您应该注意到第二个参数是一个数组:array('content'=>$output)
renderfile()是CBaseController的一种方法,在某些时候,它将调用
public function renderInternal($_viewFile_, $_data_ = null, $_return_ = false) {
// we use special variable names here to avoid conflict when extracting data
if (is_array($_data_))
extract($_data_, EXTR_PREFIX_SAME, 'data'); // (1)
else
$data = $_data_;
if ($_return_) {
ob_start();
ob_implicit_flush(false);
require($_viewFile_); // (2)
return ob_get_clean();
}
else
require($_viewFile_);
}
这就是你的答案所在:
(1)$数据仍然是我们的数组('content'=>$输出)
。extract函数将从该数组生成并初始化变量,即$content变量
(2)现在需要布局文件$当然,内容也存在于其范围内,因为您的控制器位于$this后面