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/
  • 相关阅读:
    Linux进程组调度机制分析【转】
    Linux内存管理 (22)内存检测技术(slub_debug/kmemleak/kasan)【转】
    linux syscall 详解【转】
    Linux C 读取文件夹下所有文件(包括子文件夹)的文件名【转】
    Linux RTC驱动模型分析之rtc-sysfs.c【转】
    Linux的notifier机制在TP中的应用【转】
    Linux内存管理 (10)缺页中断处理【转】
    proc/net/dev实时网速统计实例【转】
    硬中断和软中断【转】
    Linux网络
  • 原文地址:https://www.cnblogs.com/haima/p/9577087.html
Copyright © 2011-2022 走看看