模版渲染赋值替换布局等

原创 2018-12-19 11:05:48 166
摘要:<?phpnamespace app\index\controller;use \think\facade\View;use think\Controller;class Index extends Controller{    public function index()    {        return

<?php

namespace app\index\controller;

use \think\facade\View;

use think\Controller;

class Index extends Controller

{

    public function index()

    {

        return 'ok';

    }


    //模版渲染

    public function demo1()

    {

        $name = 'Sheldon';

        return $name;

    }


    //模版赋值

    public function demo2()

    {

        //$name = 'Sheldon';

        //$this->view->assign('name',$name);

        $this->view->name = 'Sheldon Lee';

        return $this->view->fetch();


    }


    //模版过滤与替换

    public function demo3()

    {

        $this->view->assign('name','Sheldon Lee Cooper');

        $filter = function($content){

            return str_replace('Sheldon Lee Cooper','谢尔顿库珀',$content);

        };

        return $this->filter($filter)->fetch();

    }



    //模版布局

    public function demo4()

    {

       /**一、全局配置,先开启模板布局功能

         * 1. config/template.php

         * 2.'layout_on' => true, 'layout_name' ] = 'layout','layout_item'=>'{__REP__}'

          二、模版标签的形式 不是全局 在哪个文件配置哪个有。在模板文件顶部指定布局模板文件即可: {layout name="layout" /}

        **/

         // return $this->view->fetch();

         

         return $this->view->engine->layout('layout','{__TEXT__}')->fetch('index/demo4');

    }



    //模板继承

    public function demo5()

    {

        //写在block标签之外内容,会原样输出到子模板中 

        

        return $this->view->fetch();


    }

}


批改老师:韦小宝批改时间:2018-12-19 11:49:18
老师总结:写的很不错!后面可以对着实际的项目来试试哦!

发布手记

热门词条