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/
  • 相关阅读:
    仅当使用了列的列表并且 IDENTITY_INSERT 为 ON 时,才能为表'SpeType'中的标识列指定显式值
    SQL Server 创建作业系列问题
    常用WebService收集
    SQL Server中调用WebService的实例
    本地时间与格林威治时间相互转换
    $route和$router的区别?
    超详细的Canal入门,看这篇就够了!
    linux 安装redis 完整步骤
    什么是JSON
    The expression 'list' evaluated to a null value
  • 原文地址:https://www.cnblogs.com/haima/p/9577087.html
Copyright © 2011-2022 走看看