Yii框架視圖、視圖布局、視圖數(shù)據(jù)塊操作示例
本文實例講述了Yii框架視圖、視圖布局、視圖數(shù)據(jù)塊操作。分享給大家供大家參考,具體如下:
Yii 視圖
控制器方法代碼:
public function actionIndex(){ $data = array( 'name' => 'zhangsan', 'age' => 12, 'address' => array('北京市','朝陽區(qū)'), 'intro' => '我是簡介,<script>alert("123");</script>' ); return $this->renderPartial('index',$data);//第二個參數(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>簡介:<?=Html::encode($intro)?> </h2> <h2>簡介:<?=HtmlPurifier::process($intro)?> </h2>
Yii 視圖布局
控制器代碼:
//設置的布局文件 public $layout = 'common'; public function actionAbout(){ $data = array('page_name'=>'About'); //render方法會把視圖文件common的內(nèi)容放到$content當中,并顯示布局文件。 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é)論:視圖引用了公共布局文件,并且在一個視圖中調(diào)用另一個視圖文件。
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?>頁面 <?php $this->endBlock();?> <h1> Hello <?=$page_name?></h1>
視圖teacher代碼:
<h1> Hello <?=$page_name?></h1> <?php $this->beginBlock('webTitle');?> <?=$page_name?>頁面 <?php $this->endBlock();?>
總結(jié):如果需要在視圖中改變公共模板中的內(nèi)容,需要使用block方法,例如上面例子中改變了common頁面的title。
更多關(guān)于Yii相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結(jié)》、《php優(yōu)秀開發(fā)框架總結(jié)》、《smarty模板入門基礎教程》、《php面向?qū)ο蟪绦蛟O計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Yii框架的PHP程序設計有所幫助。
相關(guān)文章
PHP調(diào)用.NET的WebService 簡單實例
這篇文章主要介紹了使用PHP調(diào)用.NET的WebService的一個簡單實例,十分的簡單,有需要的小伙伴可以自己擴展下,如有疑問請留言。2015-03-03關(guān)于Laravel-admin的基礎用法總結(jié)和自定義model詳解
今天小編就為大家分享一篇關(guān)于Laravel-admin的基礎用法總結(jié)和自定義model詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10使用php發(fā)送有附件的電子郵件-(PHPMailer使用的實例分析)
本篇文章介紹了使用php發(fā)送有附件的電子郵件-(PHPMailer使用的實例分析)需要的朋友參考下2013-04-04Laravel 5.1 on SAE環(huán)境開發(fā)教程【附項目demo源碼】
這篇文章主要介紹了Laravel 5.1 on SAE環(huán)境開發(fā)方法,結(jié)合實例形式詳細分析了Laravel 5.1在SAE環(huán)境下的具體開發(fā)相關(guān)操作步驟與注意事項,需要的朋友可以參考下2016-10-10thinkPHP+ajax實現(xiàn)統(tǒng)計頁面pv瀏覽量的方法
這篇文章主要介紹了thinkPHP+ajax實現(xiàn)統(tǒng)計頁面pv瀏覽量的方法,涉及thinkPHP模板調(diào)用及數(shù)據(jù)庫讀寫相關(guān)操作技巧,需要的朋友可以參考下2017-03-03100多行PHP代碼實現(xiàn)socks5代理服務器[2]
這篇文章主要介紹了100多行PHP代碼實現(xiàn)socks5代理服務器,需要的朋友可以參考下2016-05-05