zoukankan      html  css  js  c++  java
  • 12.Yii2.0框架视图模版继承与模版相互调用

    目录

    模板渲染的两种方式

    加载视图 index.php 和 about.php 页面

    建立控制器HomeController php

    D:phpStudyWWWyiicontrollersHomeController.php

    <?php
    /**
     * Created by Haima.
     * Author:Haima
     * QQ:228654416
     * Date: 2018/9/3
     * Time: 10:30
     */
    
    namespace appcontrollers;
    
    use yiiaseController;
    
    class HomeController extends Controller
    {
        public function actionIndex()
        {
            //会自动加载父模板 D:phpStudyWWWyiiviewslayoutsmain.php
            return $this->render('index');
        }
    
        public function actionAbout()
        {
            //不会自动加载父模板
            return $this->renderpartial('about');
        }
    }
    

    新建模板 homeindex.php

    D:phpStudyWWWyiiviewshomeindex.php

    <h2>index</h2>
    

    访问效果(会自动加载父模板):

    D:phpStudyWWWyiiviewshomeabout.php

    <h2>about</h2>
    

    新建模板homeabout.php

    访问效果(不会自动加载父模板):


    模板继承属性与视图的相互调用

    建立控制器HomeController php.

    D:phpStudyWWWyiicontrollersHomeController.php

    <?php
    /**
     * Created by Haima.
     * Author:Haima
     * QQ:228654416
     * Date: 2018/9/3
     * Time: 10:30
     */
    
    namespace appcontrollers;
    
    use yiiaseController;
    
    class HomeController extends Controller
    {
        //用属性的方法定义父模板
        //会自动加载D:phpStudyWWWyiiviewslayoutshome.php文件
        public $layout = 'home';
        public function actionIndex()
        {
            //会自动加载父模板 D:phpStudyWWWyiiviewslayoutshome.php
            return $this->render('index');
        }
    
        public function actionAbout()
        {
            //会自动加载D:phpStudyWWWyiiviewslayoutshome.php文件
            return $this->render('about');
            //不会自动加载父模板
            // return $this->renderpartial('about');
        }
    }
    

    新建home.php父级模板

    D:phpStudyWWWyiiviewslayoutshome.php

    <!DOCTYPE html>
    <html>
    <head>
    	<title>home</title>
    </head>
    <body>
    <h1>this ihs home.php</h1>
    <?=$content?> <!-- 创建可编辑区域 -->
    </body>
    </html>
    

    新建模板 homeabout.php

    D:phpStudyWWWyiiviewshomeabout.php

    <h2>about</h2>
    

    访问效果:


    新建模板 homeindex.php

    D:phpStudyWWWyiiviewshomeindex.php

    <h2>index</h2>
    <?=$this->render('about');?> <!-- 加载调用同级里的about模板 -->
    

    访问效果:

    [Haima的博客] http://www.cnblogs.com/haima/
  • 相关阅读:
    洛谷P3811题解
    洛谷P3353在你窗外闪耀的星星-题解
    Map根据value来排序
    java8 groupby count
    Java反射
    maven profile环境切换
    获取nginx代理情况下的真实ip
    获取request里header的name和value
    git 删除iml文件
    java list 排序
  • 原文地址:https://www.cnblogs.com/haima/p/9577087.html
Copyright © 2011-2022 走看看