tp5模板详解

原创 2018-12-27 15:20:34 444
摘要:<?phpnamespace app\index\controller;use think\Controller;class Index extends Controller{    public function index()    {        return '<h3>欢迎来到PHP

<?php
namespace app\index\controller;

use think\Controller;

class Index extends Controller
{
   public function index()
   {
       return '<h3>欢迎来到PHP中文网学习<span style="color:red;">ThinkPHP5.1</span>框架开发</h3>';
   }
   //模板渲染及变量赋值
   public function demo1()
   {
       $name = 'peter';
//        return  view('demo1',['name' => $name]);
//        $this->assign('name',$name);
//        return $this->fetch();
       return $this->fetch('',['name' => $name]);
   }
   //模板过滤
   public function demo2(){
       /*
        * TP51删除了替换功能
        */
       $this->assign('name','张三');
       $filter = function($content){
           return str_replace('张三','李四',$content);
       };
       return $this->filter($filter)->fetch();
   }

   public function demo3(){
       /**
        * 一、全局配置
        * 1.config/temaplate.php
        *  开启全局模板布局
        * 'layout_on' => 'true',
        * 声明模板布局文件名
        * 'layout_name' =>'layout',
        * 二、模板标签进行配置
        * 1.不依赖于全局配置,在模板中直接用标签进行控制
        * 2.{layout name='布局模板名'}
        * 3.关闭布局{__NOLAYOUT__}
        *
        * 三、动态配置
        * 1.不需要再模板配置文件中进行任何配置
        * 2.不需要在当前模板中添加任何标签
        */
       //开启布局
       $this->view->engine->layout(true);
       return $this->view->fetch();
   }

   public function demo4(){
       return $this->view->fetch();
   }
}

/******************************************************************************************************************************************/


{block name='header'}
   {include file="public/_header" /}
{/block}

{block name='main'}核心区域{/block}
{block name='footer'}
   {include file="public/_footer" /}
{/block}


/**********************************************************************************************************************************************/


{extend name='base'}

{block name='main'}
   <h2 style="text-align: center;">这是子模板的主体部分</h2>
{/block}

批改老师:天蓬老师批改时间:2018-12-27 15:22:57
老师总结:开发过程中, 模板继承, 实际上用得不如文件导入普遍

发布手记

热门词条