ThinkPHP6.0 view



ThinkPHP6 View

  • The template engine supports normal tags and XML tags two ways of tags Definitions, respectively used for different purposes

Tag typeDescription
Ordinary tags are mainly used to output variables, function filtering and do some basic operations
XML tags are also called tag library tags. It mainly completes some logical judgment, control and loop output, and can be expanded

1. Operator

##/{$a/$b}%{$a%$b}{$a } or { $a}--{$a--} or {--$a}Comprehensive operation{$a $b*10 $c}##Ternary operator

controller file

public function index(){

View::assign('a',100);

View::assign(' b',21);

return View::fetch();

}

view file

<div> ;{$a $b}</div>

<div>{$a-$b}</div>

<div>{$a*$b }</div>

<div>{$a/$b}</div>

<div>{$a%$b}</div>

<div>{$a }</div>

<div>{ $a}</div>

<div>{$ a--}</div>

<div>{--$a}</div>

<div>{$c ? 'Exists' : 'No Exists'}</div>

2. Template function

Operator Example
{$a $b}
- {$a-$b}
*{$a*$b}
{$a ==$b ? 'yes' : 'no'}
MethodDescription
dateDate formatting (supports various time types)
formatString formatting
upperConvert to uppercase
lowerConvert to lowercase
firstOutput the first element of the array
lastOutput the last element of the array
defaultDefault value
rawDo not use (default) escaping
md5md5 encryption
substrIntercept string
  • Can call multiple functions

controller file

public function index(){

View: :assign('time',1576048640);

View::assign('num',10.0032);

View::assign('str','OUyangKE');

View::assign('arr',[

'Mr. Zhu',

'Ouyang Ke',

'Ximen Daguanren'

]);

return View::fetch();

}

view file

< div>{$time|date='Y-m-d H:i:s'}</div>

<div>{$num|format=' d'}</div>

<div>{$str|upper}</div>

<div>{$str|lower}</div>

<div> {$arr|first}</div>

<div>{$arr|last}</div>

<div>{$default|default="Ouyang Gram"}</div>

<div>{$str|substr=0,3}</div>

<div>{$str|md5}< ;/div>

<div>{$str|lower|substr=0,3}</div>

3. Loop tag

  • foreach The usage of the tag is very close to the PHP syntax, which is used to loop through the properties of an array or object

controller file

public function index(){

  $arr = [

                                                                                                                                                                                                                                                                             => 'Ouyang Ke'

],

                        'id' =>             'name' => 'Zhu Teacher '

],

'id' => 3,

'name' => '西门大官人'

]

];

View::assign('arr',$arr);

return View::fetch();

}

view file

{foreach $arr as $v}

  <div>

              < span>ID:{$v['id']}</span>

<span>Name: {$v['name']}</span>

</div>

{/foreach}

4. Volist loop tag

  • Result output of two-dimensional array

  • name The variable name assigned by the template

  • id The current loop variable can be named as you like

  • key subscript, starting from 1 Start, default variable i

  • offset start line number

  • length get line number

  • empty If the data is empty, display this text

view file

{volist name="arr" id="v" key="k" offset= "1" length="2"}

<div>

<span>ID: {$v['id']}</span>

       <span>Name: {$v['name']}</span>

                                                                                          ;/div>

{/volist}

5. If judgment tag

  • if

    Usage of tag Very close to PHP syntax, used for conditional judgment

    ##controller file

public function index(){

View::assign( 'status',1);

View::assign('order_status',4);

return View::fetch();

}

view file

{if $status == 1}

<div>Open</div>

{/if}

##{if $status == 0}


<div>Close</div>

{else/}

<div>Open</div>

{/if}

##{if $order_status == 0}

<div>Unpaid</div>

{elseif $order_status == 1/}

<div>Paid for shipment</div>

{elseif $order_status == 2/}

<div>Delivered and awaiting receipt</div>

{elseif $order_status == 3/ }

<div>Goods received pending comment</div>

{elseif $order_status == 4/}

<div>Completed< /div>

{/if}

6. Switch judgment tag

switch

tag usage and PHP syntax is very close, used for conditional judgment
  • view file

  • {switch $order_status}

{case 0}<div> ;Unpaid</div>{/case}

{case 1 }<div>Paid pending delivery</div>{/case}

{case 2 }

{case 4}<div>Completed</div>{/case}

{/switch}

7. Include files

  • include tag, import Template file

  • load tag, introduce resource files (css, js)

view file, put the header and Tail part file

{include file="public/header" /}

{include file="public/left" /}


{load href="/static/layui/css/layui.css" /}

{load href="/static/layui/layui.js" /}


{include file="public/tail" /}

8. Other tags

1. Conditional tags

TagDescription
inDetermine whether a variable has certain values
notinDetermine whether a variable does not exist with certain values
between Determine whether a variable exists with certain values
notbetween Judge whether a variable does not exist in certain range values ​​
present Judge whether a variable has been defined
notpresent Determine whether a variable is undefined
empty Determine whether a variable is undefined Is empty
notempty Judge whether a variable is not empty
defined Judge whether a variable is empty Whether a constant is defined
notdefined Determine whether a constant is undefined

public function index(){
    View::assign('number',100);
    View::assign('string','');
    return View::fetch();
}
{in name="number" value="99,100,101"}
    number等于99,100,101任意一个值
{/in}
{notin name="number" value="99,100,101"}
    number不等于99,100,101任意一个值
{/notin}
{between name="number" value="1,10"}
    number等于1 到 10 之间的任意一个值
{/between}
{notbetween name="number" value="1,10"}
    number不等于1 到 10 之间的任意一个值
{/notbetween}
{present name="number"}
    number已经定义
{/present}
{notpresent name="n"}
    n还没有定义
{/notpresent}
{empty name="string"}
    name为空值
{/empty}
{notempty name="string"}
    name有值
{/notempty}
{defined name="NAME"}
    NAME常量已经定义
{/defined}
{notdefined name="NAME"}
    NAME常量未定义
{/notdefined}

2. Compare tags

##TagDescriptioneq is equal to neq is not equal to gt is greater than egt Greater than or equal to lt Less than elt Less than or equal toheq Constantly equal to##nheq
Not always equal to
public function index(){
    View::assign("number",100);
    View::assign("string","欧阳克");
    return View::fetch();
}
{eq name="number" value="100"}
    number 等于 100
{/eq}
{neq name="number" value="101"}
    number 不等于 101
{/neq}
{gt name="number" value="33"}
    number 大于 33
{/gt}
{egt name="number" value="100"}
    number 大于等于 100
{/egt}
{lt name="number" value="200"}
    number 小于 200
{/lt}
{elt name="number" value="100"}
    number 小于等于 100
{/elt}
{heq name="string" value="欧阳克"}
    string 恒等于 欧阳克
{/heq}
{heq name="string" value="朱老师"}
    string 恒不等于 朱老师
{/heq}

3. Loop tag

##tagDescriptionforCounting loop
start: start value
  • end: end value
  • step: step value, default 1
  • name: loop variable Name, default i
  • {for start="1" end="50" step="5" name="i"}
        {$i}<br/>
    {/for}

    4, miscellaneous tags

##TagDescriptionOutput as is Use native php code
literal
php
{literal}
    {$name} 这里$name不会被当作变量,而是普通字符
    {/literal}
    {php}
    echo '欧阳克';
    {/php}

9. Example

controller code

namespace app\controller;

use think\facade\View;

class Index {

public function index(){

$title = 'Mall';

$login = 'Ouyang Ke';

$left = [

[

# 'Title' = & GT; 'Product Management',

'List 'id' => 1,

            'id' = & gt; 2,

'title' = & gt; 'product classification',

/

##]

##],

## [

'title' = & gt; 'user management',

'lists' = & gt; [

[

'ID' = & gt; 3 ,

                                                                                                                                                                                                                                                                                                  'id' => 4,

'title' = & gt; title' => 'User address',

                                                                                                                                                                                                                                    to gt; 'Order Management',

                ]

                                                                                                                                                                                                                                                                             ;              ],

                                                                                                                                                                                                                                                                     ],

[

# 'ID' = & GT; 9,

## 'Title' = & GT;

##                                                                                                                                                                                                                                                #                                                                                                                                                                                                          but 

##                 'price' => 189,

                  'discount' => 6,

'STATUS' = & GT; 1,

## // 'STATUS' = & GT;

##                                                                                                                                                                                                                                                                                # 'title' => 'Qiushui Yiren double-sided woolen winter clothing 2019 new women's clothing temperament suit collar contrasting color wool coat jacket for women',

'cat' => 'Women's clothing',

'price' => 699,

'discount' => 7,

'status' => 1,

// 'status' => 'Open',

                  'add_time' => '2019-12-12',

                                                                                using   use with use using                         out out out through out's       through     ’’'''‐'''‐‐‐‐‐ps'  ',

## [

'ID' = & GT; 3,

## 'Title' = & GT; 'cat' => 'Men's clothing',

'price' => 179,

'discount' => 8,

'status' => 0; '= & gt;' 1576080000 '

],

[

' ID '= & GT; 1,

##' Title '= & GT; Long-sleeved T-shirt, autumn round neck, black and white T-shirt, solid color top, bottoming shirt',

                'cat' => '男装',

                'price' => 99,

                'discount' => 9,

                'status' => 1,

                // 'status' => '开启',

                'add_time' => '2019-12-12',

                // 'add_time' => '1576080000'

            ],

        ];

        View::assign([

            'title'  => $title,

            'login' => $login,

            'left' => $left,

            'right' => $right

        ]);

        return View::fetch();

    }

}

view代码:head.html

<!DOCTYPE html>

<html>

<head>

    <title>{$title}--后台管理系统</title>

    <link rel="stylesheet" type="text/css" href="/static/layui/css/layui.css">

    <script type="text/javascript" src="/static/layui/layui.js"></script>

    <style type="text/css">

        .header{width:100%;height: 50px;line-height: 50px;background: #2e6da4;color:#ffffff;}

        .title{margin-left: 20px;font-size: 20px;}

        .userinfo{float: right;margin-right: 10px;}

        .userinfo a{color:#ffffff;}

        .menu{width: 200px;background:#333744;position:absolute;}

        .main{position: absolute;left:200px;right:0px;}


        .layui-collapse{border: none;}

        .layui-colla-item{border-top:none;}

        .layui-colla-title{background:#42485b;color:#ffffff;}

        .layui-colla-content{border-top:none;padding:0px;}


        .content span{background: #009688;margin-left: 30px;padding: 10px;color:#ffffff;}

        .content div{border-bottom: solid 2px #009688;margin-top: 8px;}

        .content button{float: right;margin-top: -5px;}

    </style>

</head>

<body>

    <div class="header">

        <span class="title"><span style="font-size: 20px;">{$title}</span>--后台管理系统</span>

        <span class="userinfo">【{$login}】<span><a href="javascript:;">退出</a></span></span>

    </div>

view代码:left.html

<div class="menu" id="menu">

    <div class="layui-collapse" lay-accordion>

        {foreach $left as $k=>$left_v}

            <div class="layui-colla-item">

                <h2 class="layui-colla-title">{$left_v.title}</h2>

                <div class="layui-colla-content {if $k==0}layui-show{/if}">

                    <ul class="layui-nav layui-nav-tree">

                        {foreach $left_v['lists'] as $lists_v}

                            <li class="layui-nav-item"><a href="index.html">{$lists_v.title}</a></li>

                        {/foreach}

                    </ul>

                </div>

            </div>

        {/foreach}

    </div>

</div>

view代码:bottom.html

</body>

</html>

<script>

    layui.use(['element','layer','laypage'], function(){

        var element = layui.element;

        var laypage = layui.laypage;

        $ = layui.jquery;

        layer = layui.layer;

        resetMenuHeight();

    });

    // 重新设置菜单容器高度

    function resetMenuHeight(){

        var height = document.documentElement.clientHeight - 50;

        $('#menu').height(height);

    }

</script>

view代码:index.html

{include file="public/head" /}

{include file="public/left" /}

<div class="main" style="padding:10px;">

    <div class="content">

        <span>商品列表</span>

        <div></div>

    </div>

                                                                                                                                                                             ;/th>

                                                                                                                                                                                                                         /th>

                                                                                                                                                                                                                             ;

                                                                                         #                                                                                              #                                                                                          <td>{$right_v .cat}</td>

                                                                                                                                                                                                                                                                                 ​

                                                                                                                                                                                                                             count/10)}

                                                                                                                                                                         </td>

                    <td>{$right_v.stock}</td>

                    <td>{if $right_v['status']==1}开启{else/}关闭{/if}</td>

                    <td>{$right_v.add_time|date='Y-m-d'}</td>

                </tr>

            {/volist}

        </tbody>

    </table>

</div>

{include file="public/bottom" /}