本文實(shí)例講述了Yii框架視圖、視圖布局、視圖數(shù)據(jù)塊操作。分享給大家供大家參考,具體如下:
Yii 視圖
控制器方法代碼:
public function actionIndex(){
$data = array(
'name' => 'zhangsan',
'age' => 12,
'address' => array('北京市','朝陽(yáng)區(qū)'),
'intro' => '我是簡(jiǎn)介,script>alert("123");/script>'
);
return $this->renderPartial('index',$data);//第二個(gè)參數(shù)賦值
}
視圖代碼:
?php
use yii\helpers\Html;
use yii\helpers\HtmlPurifier;
?>
h1>Hello index view/h1>
h2>姓名:?php echo $name;?>/h2>
h2>年齡:?=$age?>/h2>
h2>地址:?=$address[0]?> ?=$address[1]?>/h2>
h2>簡(jiǎn)介:?=Html::encode($intro)?> /h2>
h2>簡(jiǎn)介:?=HtmlPurifier::process($intro)?> /h2>
Yii 視圖布局
控制器代碼:
//設(shè)置的布局文件
public $layout = 'common';
public function actionAbout(){
$data = array('page_name'=>'About');
//render方法會(huì)把視圖文件common的內(nèi)容放到$content當(dāng)中,并顯示布局文件。
return $this->render('about',$data);
}
公共視圖common代碼:
!DOCTYPE html>
html>
head>
title>/title>
meta charset="UTF-8">
/head>
body>
h1>這是Common內(nèi)容/h1>
div>
?=$content?>
/div>
/body>
/html>
視圖about代碼,并調(diào)用了activity視圖:
h1> Hello ?=$page_name?>/h1>
?php echo $this->render('activity',array('page_name'=>'activity'));?>
視圖activity代碼:
h1> Hello ?=$page_name?>/h1>
結(jié)論:視圖引用了公共布局文件,并且在一個(gè)視圖中調(diào)用另一個(gè)視圖文件。
Yii 視圖數(shù)據(jù)塊
控制器代碼:
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內(nèi)容/h1>
div>
?=$content?>
/div>
/body>
/html>
視圖student代碼:
?php $this->beginBlock('webTitle');?>
?=$page_name?>頁(yè)面
?php $this->endBlock();?>
h1> Hello ?=$page_name?>/h1>
視圖teacher代碼:
h1> Hello ?=$page_name?>/h1>
?php $this->beginBlock('webTitle');?>
?=$page_name?>頁(yè)面
?php $this->endBlock();?>
總結(jié):如果需要在視圖中改變公共模板中的內(nèi)容,需要使用block方法,例如上面例子中改變了common頁(yè)面的title。
更多關(guān)于Yii相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門(mén)及常用技巧總結(jié)》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《smarty模板入門(mén)基礎(chǔ)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:- Yii框架的布局文件實(shí)例分析
- PHP的Yii框架中View視圖的使用進(jìn)階
- PHP的Yii框架中創(chuàng)建視圖和渲染視圖的方法詳解
- Yii控制器中操作視圖js的方法
- Yii視圖操作之自定義分頁(yè)實(shí)現(xiàn)方法
- YII視圖整合kindeditor擴(kuò)展的方法
- Yii2框架視圖(View)操作及Layout的使用方法分析
- Yii視圖CGridView實(shí)現(xiàn)操作按鈕定義地址示例
- Yii視圖CGridView列表用法實(shí)例分析
- YII框架學(xué)習(xí)筆記之命名空間、操作響應(yīng)與視圖操作示例
- Yii框架布局文件的動(dòng)態(tài)切換操作示例