[thinkphp6 learning process] ThinkPHP6 view

ThinkPHP6 view

  • The template engine supports two kinds of tag definitions: ordinary tags and XML tags, which are used for different purposes
label type describe
normal label Mainly used for output variables, function filtering and doing some basic operations
XML tags Also known as tag library tags, it mainly completes some logical judgments, control and loop output, and can be expanded

1. Operators (self-study)

operator example
+ { a + a+ a+b}
- { a − a- ab}
* { a ∗ a* ab}
/ { a / a/ a/b}
% { a a% ab}
++ { $ a++} or {++$a}
{$ a–} or {–$a}
Composite calculation { a + a+ a+b*10+$c}
ternary operator { a = = a== a==b ? ‘yes’ : ‘no’}

controller file

public function index(){
    
    
    View::assign('a',100);
    View::assign('b',21);
    return View::fetch();
}
<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 ? '存在' : '不存在'}</div>

2. Template function (self-study)

method describe
date Date formatting (supports various time types)
format string formatting
upper convert to uppercase
lower convert to lowercase
first output the first element of the array
last output the last element of the array
default Defaults
raw No (default) escaping is used
md5 md5 encryption
substr intercept string
  • Multiple function calls are possible

controller file

public function index(){
    
    
    View::assign('time',1576048640);
    View::assign('num',10.0032);
    View::assign('str','OUyangKE');
    View::assign('arr',[
        '雪碧',
        '可口可乐',
        '红酒 '
    ]);
    return View::fetch();
}

view file

<div>{$time|date='Y-m-d H:i:s'}</div>
<div>{$num|format='%02d'}</div>
<div>{$str|upper}</div>
<div>{$str|lower}</div>
<div>{$arr|first}</div>
<div>{$arr|last}</div>
<div>{$default|default="cafe"}</div>
<div>{$str|substr=0,3}</div>
<div>{$str|md5}</div>
<div>{$str|lower|substr=0,3}</div>

3. Cycle label

  • The usage of the foreach tag is very close to the PHP syntax, which is used to loop out the properties of arrays or objects

controller file

public function index(){
    
    
    $arr = [
        [
            'id' => 1,
            'name' => '可口可乐'
        ],
        [
            'id' => 2,
            'name' => '叶大牛'
        ],
        [
            'id' => 3,
            'name' => '官人快来'
        ]
    ];
    View::assign('arr',$arr);
    return View::fetch();
}

view file

{foreach $arr as $v}
    <div>
        <span>ID:{$v['id']}</span>
        <span>姓名:{$v['name']}</span>
    </div>
{/foreach}

4. volist cycle label

  • The result output of the two-dimensional array
  • name variable name template assignment
  • id The current loop variable, you can name it at will
  • key subscript, starting from 1, default variable i
  • offset start line number
  • length Get the number of rows
  • 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>姓名:{$v['name']}</span>
        <span>下标:{$k}</span>
    </div>
{/volist}

Five, if judgment label

  • The usage of the if tag is very close to the PHP syntax and is 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>开启</div>
{/if}

{if $status == 0}
    <div>关闭</div>
{else/}
    <div>开启</div>
{/if}

{if $order_status == 0}
    <div>未支付</div>
{elseif $order_status == 1/}
    <div>已支付 待发货</div>
{elseif $order_status == 2/}
    <div>已发货 待收货</div>
{elseif $order_status == 3/}
    <div>已收货 待评论</div>
{elseif $order_status == 4/}
    <div>已完成</div>
{/if}

6. Switch judgment label

  • The usage of the switch tag is very close to the PHP syntax and is used for conditional judgment

view file

{switch $order_status}
    {case 0 }<div>未支付</div>{/case}
    {case 1 }<div>已支付 待发货</div>{/case}
    {case 2 }<div>已发货 待收货</div>{/case}
    {case 3 }<div>已收货 待评论</div>{/case}
    {case 4 }<div>已完成</div>{/case}
{/switch}

7. Include files

  • include tag, importing template files
  • load tag, import resource files (css, js)

View file, the header and tail part of the 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 labels (self-study)

1. Condition label

Label describe
in Check if a variable has some value
swimming Determine if a variable does not have some value
between Check if a variable has some value
notbetween Determine whether a variable does not have certain range values
present Check if a variable is defined
notpresent Check if a variable is undefined
empty Check if a variable is empty
notempty Check if a variable is not empty
defined Check if a constant is defined
notdefined Check if 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等于99100101任意一个值
{
    
    /in}
{
    
    notin name="number" value="99,100,101"}
    number不等于99100101任意一个值
{
    
    /notin}

{
    
    between name="number" value="1,10"}
    number等于110 之间的任意一个值
{
    
    /between}
{
    
    notbetween name="number" value="1,10"}
    number不等于110 之间的任意一个值
{
    
    /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}
  • Conditional tags, you can add else tags
{
    
    in name="number" value="99,100,101"}
    number等于99100101任意一个值
{
    
    else/}
    number不等于99100101任意一个值
{
    
    /in}

2. Compare labels

Label describe
eq equal
neq not equal to
gt more than the
egt greater or equal to
lt less than
elt less than or equal to
REMOVES equal to
nheq constant is not 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. Cycle label

Label describe
for count 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 tab

Label describe
literal Output as-is, preventing template tags from being parsed
php Using native php code, you can write any PHP statement code in the template file
{literal}
    {$name} 这里$name不会被当作变量,而是普通字符
{/literal}

{php}
    echo '可口可乐搬代码';
{/php}

9. Examples

controller code

namespace app\controller;
use think\facade\View;
class Index{
    
    
    public function index(){
    
    
        $title = '商城';
        $login = '欧阳克';
        $left = [
            [
                'title' => '商品管理',
                'lists' => [
                    [
                        'id' => 1,
                        'title' => '商品列表',
                    ],
                    [
                        'id' => 2,
                        'title' => '商品分类',
                    ]
                ]
            ],
            [
                'title' => '用户管理',
                'lists' => [
                    [
                        'id' => 3,
                        'title' => '用户列表',
                    ],
                    [
                        'id' => 4,
                        'title' => '购物车',
                    ],
                    [
                        'id' => 5,
                        'title' => '用户地址',
                    ],
                    [
                        'id' => 6,
                        'title' => '订单管理',
                    ]
                ]
            ],
            [
                'title' => '后台管理',
                'lists' => [
                    [
                        'id' => 7,
                        'title' => '管理员列表',
                    ],
                    [
                        'id' => 8,
                        'title' => '个人中心',
                    ],
                    [
                        'id' => 9,
                        'title' => '左侧菜单列',
                    ]
                ]
            ]
        ];
        $right = [
            [
                'id' => 1,
                'title' => '熙世界2019秋冬新款长袖杏色上衣连帽宽松刺绣文艺落肩袖加厚卫衣BF风',
                'cat' => '女装',
                'price' => 189,
                'discount' => 6,
                'status' => 1,
                // 'status' => '开启',
                'add_time' => '2019-12-12',
                // 'add_time' => '1576080000'
            ],
            [
                'id' => 2,
                'title' => '秋水伊人双面呢冬装2019年新款女装气质西装领撞色羊毛大衣外套女',
                'cat' => '女装',
                'price' => 699,
                'discount' => 7,
                'status' => 1,
                // 'status' => '开启',
                'add_time' => '2019-12-12',
                // 'add_time' => '1576080000'
            ],
            [
                'id' => 3,
                'title' => '微弹中高腰直脚牛仔裤男',
                'cat' => '男装',
                'price' => 179,
                'discount' => 8,
                'status' => 0,
                // 'status' => '开启',
                'add_time' => '2019-12-12',
                // 'add_time' => '1576080000'
            ],
            [
                'id' => 1,
                'title' => '男士长袖t恤秋季圆领黑白体恤T 纯色上衣服打底衫',
                '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 code: 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 code: 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 code: 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 code: index.html

{include file="public/head" /}
{include file="public/left" /}
<div class="main" style="padding:10px;">
    <div class="content">
        <span>商品列表</span>
        <div></div>
    </div>
    <table class="layui-table">
        <thead>
            <tr>
                <th>ID</th>
                <th>商品标题</th>
                <th>分类</th>
                <th>原价</th>
                <th>折扣</th>
                <th>现价</th>
                <th>库存</th>
                <th>状态</th>
                <th>添加时间</th>
            </tr>
        </thead>
        <tbody>
            {volist name="right" id="right_v"}
                <tr>
                    <td>{$right_v.id}</td>
                    <td>{$right_v.title}</td>
                    <td>{$right_v.cat}</td>
                    <td>{$right_v.price}</td>
                    <td>{$right_v.discount}</td>
                    <td>
                        {if $right_v.discount!=0}
                            {$right_v.price*($right_v.discount/10)}
                        {else/}
                            {$right_v.price}
                        {/if}
                    </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" /}

Guess you like

Origin blog.csdn.net/qzmlyshao/article/details/131013777