thinkphp5-模板

模板基本使用

控制器 application/index/controller/index.php

<?php
namespace app\index\controller;

use think\Controller;

class Index extends Controller
{

    public function index()
    {
        return $this->fetch('index',[
            'name'=>'huyongjian',
            'age'=>35
        ]);
     }

}

视图 application/index/view/index/index.html

{$name}

变量输出

控制器

$view = new View();
$data['name'] = 'huyongjian';
$data['email'] = '308830232@qq.com';
$view->assign('data',$data);

return $view->fetch('index');

视图

Name:{$data.name}<br>
Email:{$data.email}

系统变量

控制器

session('user_id','sess_student_3');
cookie('name','胡勇健',600);

return $view->fetch('index');

视图模板

{$Think.server.script_name}<br>
{$Think.session.user_id}<br>
{$Think.get.page}<br>
{$Think.cookie.name}

<hr>
{$Think.const.APP_PATH}<br>
{$Think.APP_PATH}

<hr>
{$Think.config.default_module}

测试

http://www.tp5.com/index.php/index/index/index?page=2

获取Request请求数据

控制器

$view = new View();
return $view->fetch('index');

模板

{$Request.get.id}<br>
{$Request.param.name}<br>
{$Request.root}<br>
{$Request.root.true}<br>
{$Request.path}<br>
{$Request.module}<br>
{$Request.controller}<br>
{$Request.action}<br>
{$Request.host}<br>
{$Request.ip}<br>
{$Request.header.connection}

测试

http://www.tp5.com/index.php/index/index/index/name/huyongjian?id=2

结果

2
huyongjian
/index.php
http://www.tp5.com/index.php
index/index/index/name/huyongjian
index
Index
index
www.tp5.com
127.0.0.1
keep-alive

使用函数

控制器

$view = new View();
return $view->fetch('index',['name'=>'huyongjian','create_time'=>time()]);     

视图

{$name|md5}<br>
{$name|substr=0,6}<br>
{$name|md5|strtoupper|substr=0,10}<br>
{$create_time|date="Y-m-d H:i:s",###}

测试结果

4d8b497cedde8a5bcc039637b6da1710
huyong
4D8B497CED
2021-11-01 14:39:45

变量默认值

{$user.nickname|default='默认名称'}

运算符

控制器

$view = new View();
return $view->fetch('index',['a'=>20,'b'=>2]);

视图

{$a + $b}<br>
{$a - $b}<br>
{$a * $b}<br>
{$a / $b}<br>
{$a % $b}<br>
{$a++}<br>
{$a--}<br>
{$a + $b * 3}<br>

结果

22
18
40
10
0
20
21
26

三元运算符

{$status? '正常' : '错误'}

原样输出

{literal}
    Hello,{$name}!
{/literal}

模板注释

{/* 注释内容 */} 或 {// 注释内容 }

模板布局

全局配置

config.php template配置添加

'layout_on'     =>  true,
'layout_name'   =>  'layout',

控制器 application/index/controller/index.php

<?php

namespace app\index\controller;

use think\Controller;

class Index extends Controller
{
    public function index()
    {
        return $this->fetch('index');
     }

     public function add(){
        return $this->fetch('add');
     }
}

布局文件 application/index/view/layout.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
          integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"
            integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd"
            crossorigin="anonymous"></script>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-12" style=" height: 50px; color: white; text-align: center">
            header
        </div>
    </div>
    <div class="row" style="height: 100%;">
        <div class="col-md-2" style="color: white; text-align: center;height: 100%">
            menu
        </div>
        <div class="col-md-10">{__CONTENT__}</div>
    </div>
    <div class="row">
        <div class="col-md-12" style=" text-align: center;color: white;">
            footer
        </div>
    </div>
</div>
</body>
</html>

视图 application/index/view/index.php

index

视图 application/index/view/add.php

add

测试

http://www.tp5.com/index.php/index/index/index
http://www.tp5.com/index.php/index/index/add
模板直接调用

视图

{layout name="layout" /}
控制器直接调用
public function index()
    {
        $this->view->engine->layout('layout');
        return $this->fetch('index');
     }

模板继承

控制器 application/index/controller/index.php

<?php

namespace app\index\controller;

use think\Controller;

class Index extends Controller
{
    public function index()
    {
        $list = [
            ['id'=>1,'title'=>'title','content'=>'content'],
            ['id'=>2,'title'=>'title2','content'=>'content2'],
            ['id'=>3,'title'=>'title3','content'=>'content3'],
        ];
        $news = [
            ['id'=>1,'title'=>'title'],
            ['id'=>2,'title'=>'title2'],
            ['id'=>3,'title'=>'title3'],
        ];
        $this->assign('list',$list);
        $this->assign('news',$news);
        $this->view->engine->layout('layout');
        return $this->fetch('index');
     }
}

base布局 application/index/view/base.html

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>{block name="title"}标题{/block}</title>
</head>
<body>
{block name="menu"}菜单{/block}
{block name="left"}左边分栏{/block}
{block name="main"}主内容{/block}
{block name="right"}右边分栏{/block}
{block name="footer"}底部{/block}
</body>
</html>

layout布局 application/index/view/layout.html

{extend name="layout" /}

{block name="title"}{$title|default='title'}{/block}
{block name="menu"}
<a href="/" >首页</a>
<a href="/info/" >资讯</a>
<a href="/bbs/" >论坛</a>
{/block}
{block name="left"}{/block}
{block name="main"}
{volist name="list" id="vo"}
<a href="/new/{$vo.id}">{$vo.title}</a><br/>
{$vo.content}
{/volist}
{/block}
{block name="right"}
最新资讯:
{volist name="news" id="new"}
<a href="/new/{$new.id}">{$new.title}</a><br/>
{/volist}
{/block}
{block name="footer"}
{__block__}
@ThinkPHP 版权所有
{/block}

测试结果

首页 资讯 论坛 title
contenttitle2
content2title3
content3 最新资讯: title
title2
title3
@ThinkPHP 版权所有

包含文件

{include file="public/header" /}

内置标签

循环标签

{volist name="list" id="vo"}
    {$vo.id}:{$vo.title}<br/>
{/volist}
{foreach $list as $vo}
    {$vo.id}:{$vo.title}<br>
{/foreach}

比较标签

{eq name="name" value="value"}value{/eq}
{eq name="name" value="value"}
相等
{else/}
不相等
{/eq}

标签

标签    含义
eq或者 equal    等于
neq 或者notequal    不等于
gt    大于
egt    大于等于
lt    小于
elt    小于等于
heq    恒等于
nheq    不恒等于

条件判断

{switch name="User.level"}
    {case value="1"}value1{/case}
    {case value="2"}value2{/case}
    {default /}default
{/switch}
{if condition="$User.level == 1"} 
    value1
{elseif condition="$User.level eq 2"/}
    value2
{else /} 
    default
{/if}

加载资源文件

<script type='text/javascript' src='/static/js/common.js'>
<link rel="stylesheet" type="text/css" href="/static/css/style.css" />
{load href="/static/js/common.js" /}
{load href="/static/css/style.css" /}

原生php

{php}
    switch($User['level']){
        case 1:
            echo 'value1';
            break;
        case 2:
            echo 'value2';
            break;
        defalut:
            echo 'defalut';
}
{/php}

模板定义变量

{assign name="name" value="huyongjian" /}
{$name}

模板定义常量

{define name="APP_CONFIG_PATH" value="application/config/" /}
{$Think.const.APP_CONFIG_PATH}
posted @ 2021-11-02 15:39  胡勇健  阅读(253)  评论(0)    收藏  举报