ThinkPHP6.0-Ansicht



Denken Sie an PHP6 View

  • Template Engine unterstützt zwei Tag-Definitionen, 普通标签 und XML标签, die jeweils für unterschiedliche Zwecke verwendet werden

标签类型描述
普通标签主要用于输出变量、函数过滤和做一些基本的运算操作
XML标签也称为标签库标签,主要完成一些逻辑判断、控制和循环输出,并且可扩展

1. Operator

运算符示例
{$a $b}
-{$a-$b}
*{$a*$b}
/{$a/$b}
%{$a%$b}
{$a } 或 { $a}
--{$a--} 或{--$a}
综合运算{$a $b*10 $c}
三元运算符{$a==$b ? 'yes' : 'no'}

controller文件

public function index(){

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

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

    zurück View::fetch();

}

view文件

<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 }</di v>

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

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

<div>{$c ? '存在' : '不存在'}</div>

 二、模版函数 

方法描述
date日期格式化(支持各种时间类型)
format字符串格式化
upper转换为大写
lower转换为小写
first输出数组的第一个元素
last输出数组的最后一个元素
default默认值
raw不使用(默认)转义
md5md5加密
substr截取字符串
  • Kann mehrere Funktionen aufrufen

Controller-Datei

öffentlicher Funktionsindex(){

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

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

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

View::assign('arr',[

'Teacher Zhu',

'Ouyang Ke',

'Ximen Official'

]) ;

Zurück View::fetch();

}

Datei anzeigen

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

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

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

<div>{$st r|lower}</div>

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

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

<div>{$default|de fault="Ouyang Ke"}</div>

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

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

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

3. Verwendung von Loop-Tags

  • foreach Tags Die Methode kommt der PHP-Syntax sehr nahe und wird verwendet, um die Eigenschaften eines Arrays oder Objekts

Controller-Datei

öffentlich zu durchlaufen Funktionsindex () {

$ arr = [

[

'ID' = & gt;

<🎜 f '& gt; 'Ouyang Ke'

                                                      'Lehrer Zhu'

                                                             'Ximen-Beamter'

]

];

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

return View::fetch();

}

Datei anzeigen

{foreach $arr as $v}

< div>

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

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

</div>

{/foreach

4. Volist-Loop-Label

  • Die Ergebnisausgabe eines zweidimensionalen Arrays

  • Name Der von der Vorlage zugewiesene Variablenname

  • id Die aktuelle Schleifenvariable kann beliebig benannt werden

  • Schlüsselindex, beginnend mit 1, der Standardvariablen i

  • Offset Startzeilennummer

  • Länge Zeilennummer abrufen

  • leer Wenn die Daten leer sind, zeigen Sie diesen Text an

Datei anzeigen

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

<div>

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

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

  <span>Index: {$k}</span>

</div>

{/volist}

5 Die Verwendung des Beurteilungs-Tags

  • -Tag kommt der PHP-Syntax sehr nahe und wird für die bedingte Beurteilung if

Controller-Datei < verwendet 🎜>

public function index(){

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

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

return View::fetch();

}

Datei anzeigen

{if $status == 1}

<div>Öffnen</div>

{/if}


{if $status == 0}

<div>Schließen</div>

{else/}

<div>Öffnen</div>

{/if}


{if $order_status == 0}

<div>Nicht bezahlt</div>

{elseif $order_status == 1/}

<div>Bezahlt Warten auf Versand</div>

{elseif $order_status == 2/}

{elseif $order_status == 3/}

<div>Empfangen, wartet auf Kommentar</div>

{elseif $order_status == 4/}

<div>Abgeschlossen</div>

{/if}

6 Die Verwendung des Beurteilungs-Tags

ähnelt sehr der PHP-Syntax, die für die bedingte Beurteilung
  • switch Ansichtsdatei verwendet wird

{switch $order_status}

{case 0 }<div>Unbezahlt</div>{/case}

{case 1 }<div>Für den Versand bezahlt</div>{/case}

{case 2} <div>versandt Warte auf Empfang</div>{/case}

{case 3}<div>Empfangen ausstehender Kommentar</div>{/case}

{case 4} <div>Abgeschlossen< ;/div>{/case}

{/switch}

7. Fügen Sie das Datei-Tag

  • include hinzu und führen Sie die Vorlagendatei ein

  • load Tag, Ressourcendateien (CSS, JS) einführen

Datei anzeigen, Kopf- und Endteildateien einfügen

{ enthalten file="public/header" /}

{include file="public/left" /}


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

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


{include file="public/tail" /}

Andere Tags

1 Schlagworte

标签描述
in判断变量是否存在某些值
notin判断变量是否不存在某些值
between 判断变量是否存在某些值
notbetween 判断变量是否不存在某些范围值
present 判断某个变量是否 已定义
notpresent 判断某个变量是否 未定义
empty 判断某个变量是否为空
notempty 判断某个变量是否不为空
defined 判断某个常量是否 已定义
notdefined 判断某个常量是否 未定义

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. Vergleichs-Tags

标签描述
eq等于
neq 不等于
gt 大于
egt 大于等于
lt小于
elt 小于等于
heq 恒等于
nheq 不恒等于

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. Schleifen-Tags

标签描述
for计数循环
    Start : Startwert
  • Ende: Endwert
  • Schritt: Schrittwert, Standard 1
  • Name: Schleifenvariablenname, Standard i
  • {for start="1" end="50" step="5" name="i"}
        {$i}<br/>
    {/for}
  • Verschiedene Tags

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

9. Beispiel

Controller-Code

Namespace AppController;

ThinkfacadeView verwenden;

Klassenindex{

öffentliche Funktion index(){

              $title = 'Mall';

            $login = 'Ouyang Ke';                    [

                                                                                                                                                                                          [

                                                                                                                                                                                                                                                                                                                                                                                                        'title' => 'Produktkategorie',

]

]

],

                                                                                                                                                             [

                                                                                                                                                                                                                                                                                                                                                                                                            'title' => 'Warenkorb',

                                                                             'id' => 5,

'title' =>

                                                                                                                                                                     5. ]

                                                                                                                                                 'Listen' => 'title' => 'Administratorliste',

                                                                    'id' => 8,

                                                                                                                                                                       'title' => [

<'> 'ID' = & gt; 9,

'title' = & gt;                                                                                                                                      [

                                                                                                                                                                                                                                     „Xi World 2019 Herbst und Winter Neues langärmliges Aprikosen-Oberteil mit Kapuze, locker besticktes Art-Drop-Ärmel-verdicktes Sweatshirt im BF-Stil“,

„Katze“ => „Damenbekleidung“,

„Preis“ = > 189,

                                                                                            

                     'status' => '2019-12-12',

                                                                                                                  [

<'> 'ID' = & gt; 2,

'title' = & gt;

                                                                                                                                                                                                                   7,

                                                                                                                                                                                                           => '2019-12-12',

                                                     [

                                                                                                                                                                                                                         => 'Herrenbekleidung',

'Preis' =>

'Rabatt' => 'status' => 0,

                                                                                                                                                                                                                                                  🎜>                                                => 1,

'title' => 'Langarm-T-Shirt für Herren mit Rundhalsausschnitt in Schwarz und Weiß',

                'cat' => '男装',

                'price' => 99,

                'Rabatt' => 9,

                'status' => 1,

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

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

                // 'add_time' => '1576080000'

            ],

        ];

        View::assign([

            'title'  => $title,

            'login' => $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: 20 Pixel; 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-navlayui-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>

Code anzeigen: bottom.html

</body>

</html>

<script>

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

var element =layui.element;

varlaypage =layui.laypage;

$ = layui.jquery;

           layer =layui.layer; resetMenuHeight(){

var height = document.documentElement.clientHeight - 50;

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

}

</script>

Code anzeigen: index.html

{include file="public/head" /}

{include file="public/left" /}

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

<div class="content">


< span>Produktliste</span>

<div></div>

</div>

                                                                             <th>ID</th>

                                                                                          <th>Kategorie</th>

                                                                              <th>Rabatt</th>

                                                                                    <th>Inventar</th>

                                                             <th>Hinzufügen. Zeit</th>

                                                     <tbody>

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

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

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

                                                                                             $right_v.discount!=0}

                                                                         {else/}

                                                                                                       </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" /}