亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

本文實(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框架入門及常用技巧總結(jié)》、《php優(yōu)秀開發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總

希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論