YII Framework学习教程-YII的V-view的layout

通常,我们把一个html页面分成若干个部分,例如header、body、footer。我们这种划分在YII中可以通过layout来实现。layout相当有为我们提供了一个通用的页面风格。具体的内容需要我们根据具体情况来实现。这样可以保证页面的风格统一。减少代码的冗余,结构上的更改只需修改layout就可以。

    当使用render()的时候,是默认是用protected/views/layouts/main.php来作为layout文件的。

  

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">  
  3. <head>  
  4.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5.     <meta name="language" content="utf-8" />  
  6.   
  7.     <!-- blueprint CSS framework -->  
  8.     <link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/screen.css" media="screen, projection" />  
  9.     <link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/print.css" media="print" />  
  10.     <!--[if lt IE 8]>  
  11.     <link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/ie.css" media="screen, projection" />  
  12.     <![endif]-->  
  13.   
  14.     <link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/main.css" />  
  15.     <link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/form.css" />  
  16.   
  17.     <title><?php echo CHtml::encode($this->pageTitle); ?></title>  
  18. </head>  
  19.   
  20. <body>  
  21.   
  22. <div class="container" id="page">  
  23.   
  24.     <div id="header">  
  25.         <div id="logo"><?php echo CHtml::encode(Yii::app()->name); ?></div>  
  26.     </div><!-- header -->  
  27.   
  28.     <div id="mainmenu">  
  29.         <?php $this->widget('zii.widgets.CMenu',array(  
  30.             'items'=>array(  
  31.                 array('label'=>'Home', 'url'=>array('/site/index')),  
  32.                 array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')),  
  33.                 array('label'=>'Contact', 'url'=>array('/site/contact')),  
  34.                 array('label'=>'Login', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest),  
  35.                 array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest)  
  36.             ),  
  37.         )); ?>  
  38.     </div><!-- mainmenu -->  
  39.     <?php if(isset($this->breadcrumbs)):?>  
  40.         <?php $this->widget('zii.widgets.CBreadcrumbs', array(  
  41.             'links'=>$this->breadcrumbs,  
  42.         )); ?><!-- breadcrumbs -->  
  43.     <?php endif?>  
  44.   
  45.     <?php echo $content; ?>  
  46.   
  47.     <div id="footer">  
  48.         Copyright © <?php echo date('Y'); ?> by My Company.<br/>  
  49.         All Rights Reserved.<br/>  
  50.         <?php echo Yii::powered(); ?>  
  51.     </div><!-- footer -->  
  52.   
  53. </div><!-- page -->  
  54.   
  55. </body>  
  56. </html>  
       可以通过改变 CController::layout 或者 CController::layout 进行自定义页面layout。

            如果要渲染一个使用layout,则需调用 renderPartial() 。

            如果要改变某一个action的layout,可以使用 $this->layout来指定要使用的layout

  1. /** 
  2.  * This is the default 'index' action that is invoked 
  3.  * when an action is not explicitly requested by users. 
  4.  */  
  5. public function actionIndex()  
  6. {  
  7.     $viewData=array();  
  8.       
  9.     // renders the view file 'protected/views/site/index.php'  
  10.     // using the default layout 'protected/views/layouts/main.php'  
  11.     $viewData['homeUrl'] = Yii::app()->homeUrl;        
  12.     $viewData['var1'] = '这是var1变量的对应的值';  
  13.     $this->layout='mylayout';  
  14.     $this->render('index',$viewData);  
  15.       
  16. }  

          如果要改变某一个controller的layout,可以实现init方法,来改变layout模板。如下

          

  1. <?php  
  2.   
  3. class SiteController extends Controller  
  4. {  
  5.     public function init()  
  6.     {  
  7.         $this->layout='mylayout';  
  8.     }  
      这样,此contrller的所用action就会默认应用mylayout模板。

      如果不是用layout可以使用$this->renderPartial('index',$viewData);

      

  1. /** 
  2.  * This is the default 'index' action that is invoked 
  3.  * when an action is not explicitly requested by users. 
  4.  */  
  5. public function actionIndex()  
  6. {  
  7.     $viewData=array();  
  8.       
  9.     // renders the view file 'protected/views/site/index.php'  
  10.     // using the default layout 'protected/views/layouts/main.php'  
  11.     $viewData['homeUrl'] = Yii::app()->homeUrl;        
  12.     $viewData['var1'] = '这是var1变量的对应的值';  
  13.     $this->layout='mylayout';  
  14.     $this->renderPartial('index',$viewData);  
  15.       
  16. }  
         

  如果要禁用layout可以 $this->layout=false;          


 以后在开发中慢慢体会layout的用法和它的强大。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值