欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Yii框架視圖、視圖布局、視圖數(shù)據(jù)塊操作示例

 更新時間:2019年10月14日 09:58:17   作者:學知無涯  
這篇文章主要介紹了Yii框架視圖、視圖布局、視圖數(shù)據(jù)塊操作,結(jié)合實例形式分析了Yii框架相關(guān)的視圖、布局、控制器及數(shù)據(jù)相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了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)文章

最新評論