通常,我们把一个html页面分成若干个部分,例如header、body、footer。我们这种划分在YII中可以通过layout来实现。layout相当有为我们提供了一个通用的页面风格。具体的内容需要我们根据具体情况来实现。这样可以保证页面的风格统一。减少代码的冗余,结构上的更改只需修改layout就可以。
当使用render()的时候,是默认是用protected/views/layouts/main.php来作为layout文件的。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="language" content="utf-8" />
<!-- blueprint CSS framework -->
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/screen.css" media="screen, projection" />
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/print.css" media="print" />
<!--[if lt IE 8]>
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/ie.css" media="screen, projection" />
<![endif]-->
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/main.css" />
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/form.css" />
<title><?php echo CHtml::encode($this->pageTitle); ?></title>
</head>
<body>
<div class="container" id="page">
<div id="header">
<div id="logo"><?php echo CHtml::encode(Yii::app()->name); ?></div>
</div><!-- header -->
<div id="mainmenu">
<?php $this->widget('zii.widgets.CMenu',array(
'items'=>array(
array('label'=>'Home', 'url'=>array('/site/index')),
array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')),
array('label'=>'Contact', 'url'=>array('/site/contact')),
array('label'=>'Login', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest),
array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest)
),
)); ?>
</div><!-- mainmenu -->
<?php if(isset($this->breadcrumbs)):?>
<?php $this->widget('zii.widgets.CBreadcrumbs', array(
'links'=>$this->breadcrumbs,
)); ?><!-- breadcrumbs -->
<?php endif?>
<?php echo $content; ?>
<div id="footer">
Copyright © <?php echo date('Y'); ?> by My Company.<br/>
All Rights Reserved.<br/>
<?php echo Yii::powered(); ?>
</div><!-- footer -->
</div><!-- page -->
</body>
</html>
可以通过改变 CController::layout 或者 CController::layout 进行自定义页面layout。
如果要渲染一个使用layout,则需调用 renderPartial() 。
如果要改变某一个action的layout,可以使用 $this->layout来指定要使用的layout
/**
* This is the default 'index' action that is invoked
* when an action is not explicitly requested by users.
*/
public function actionIndex()
{
$viewData=array();
// renders the view file 'protected/views/site/index.php'
// using the default layout 'protected/views/layouts/main.php'
$viewData['homeUrl'] = Yii::app()->homeUrl;
$viewData['var1'] = '这是var1变量的对应的值';
$this->layout='mylayout';
$this->render('index',$viewData);
}
如果要改变某一个controller的layout,可以实现init方法,来改变layout模板。如下
<?php
class SiteController extends Controller
{
public function init()
{
$this->layout='mylayout';
}
这样,此contrller的所用action就会默认应用mylayout模板。
如果不是用layout可以使用$this->renderPartial('index',$viewData);
/**
* This is the default 'index' action that is invoked
* when an action is not explicitly requested by users.
*/
public function actionIndex()
{
$viewData=array();
// renders the view file 'protected/views/site/index.php'
// using the default layout 'protected/views/layouts/main.php'
$viewData['homeUrl'] = Yii::app()->homeUrl;
$viewData['var1'] = '这是var1变量的对应的值';
$this->layout='mylayout';
$this->renderPartial('index',$viewData);
}
如果要禁用layout可以 $this->layout=false;
以后在开发中慢慢体会layout的用法和它的强大。