zoukankan      html  css  js  c++  java
  • 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。

  • 相关阅读:
    2017-12 CDQZ集训(已完结)
    BZOJ1492 货币兑换 CDQ分治优化DP
    BZOJ2001 [Hnoi2010]City 城市建设 CDQ分治
    树套树小结
    跑路了
    NOI2020 游记
    半平面交模板
    Luogu 3245 大数
    Luogu 3246 序列
    test20190408(十二省联考)
  • 原文地址:https://www.cnblogs.com/gyfluck/p/9100573.html
Copyright © 2011-2022 走看看