Yii 之视图数据块
控制器代码:
public $layout = 'common'; public function actionStudent(){ $data = array('page_name'=>'Student'); return $this->render('student',$data); } public function actionTeacher(){ $data = array('page_name'=>'Teacher'); return $this->render('teacher',$data); }
公共布局文件common代码:
<!DOCTYPE html>
<html>
<head>
<title>
<?php if(isset($this->blocks['webTitle'])):?>
<?=$this->blocks['webTitle'];?>
<?php else:?>
commom
<?php endif;?>
</title>
<meta charset="UTF-8">
</head>
<body>
<h1>这是Common内容</h1>
<div>
<?=$content?>
</div>
</body>
</html>
视图student代码:
<?php $this->beginBlock('webTitle');?> <?=$page_name?>页面 <?php $this->endBlock();?> <h1> Hello <?=$page_name?></h1>
视图teacher代码:
<h1> Hello <?=$page_name?></h1> <?php $this->beginBlock('webTitle');?> <?=$page_name?>页面 <?php $this->endBlock();?>
总结:如果需要在视图中改变公共模板中的内容,需要使用block方法,例如上面例子中改变了common页面的title。

浙公网安备 33010602011771号