学习Thinkphp6第二课时

前言

今天我们一起继续来学习Thinkphp6的课程吧

return标签

retutn标签的作用是在页面上输出内容

<?php
namespace app\controller;

use app\BaseController;

class Index extends BaseController
{
    public function index( )
    return'清海';
}

e3fae21ef2154240

安装视图

接下来是安装视图

步骤:

  • 按住win键+R打开并输入cmd运行界面
  • 999c39769e155546
  • 然后将指令输入即可(安装视图的指令:composer require topthink/think-view)
    be97f3dc2c155555然后安装视图就完成了

使用视图

首先先在app文件夹中新建一个名为view的文件夹中再创建一个文件夹“index”,在文件夹“index”中新建文件“index.html”

6780f2cd7c160648

其次,需插入 use think\facade\View;return view: :fetch 代码

?php
namespace app\controller;
use app\BaseController;
use think\facade\view;
class Index extends Basecontroller
{
public function index()
{
return view: :fetch();
}
}

然后就可以通过我们之前学习的HTML来进行网页编写

在thinkphp6中编写以下内容

<?php
namespace app\controller;

use app\BaseController;
use think\facade\View;

class Index extends BaseController
{
    public function index( ){
        view::assign('name','清海');    
        return view::fetch();
}
}

在HTML中编写以下内容

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    {$name}
</body>
</html>

 效果如下

e3fae21ef2161747

算法标签

在thinkphp6中输入

<?php
namespace app\controller;

use think\facade\View;

class Text{
    public function index(){
        View::assign([
            'a' =>100,
            'b' =>23
        ]);
        return View::fetch();
    }
}

在HTML中输入

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    {$a + $b}<br>
    {$a - $b}<br>
    {$a * $b}<br>
    {$a / $b}<br>
    {$a % $b}<br>
    {$a ? $a : $b}<br>
</body>
</html>

效果(一一对应)如下:

9a0a0e0323164004

模板函数标签

1f8b178965164225

 

在thinkphp6中输入

<?php
namespace app\controller;

use think\facade\View;

class Text{
    public function index(){
        view::assign('time',1576048640);
        view::assign('num',10.0032);
        view::assign('str','Qinghai');
        view::assign('arr', [
        '山海',
        '清海',
        'Mr.林'
    ]);
        return view::fetch();
    }
}

在HTML中输入

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <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="清海"}</div>
    <div>{$str|substr=0,3}</div>
    <div>{$str|md5}</div>
    <div>{$str|lower|substr=0,3}</div>    
</body>
</html>

效果(一一对应)如下:

f4d8a5a797170114

foreach标签

foreach标签是循环标签

在thinkphp6中输入

<?php
namespace app\controller;

use think\facade\View;

class Text{
    public function index(){
        $arr = [
        [
            'id' => 1,
            'name' =>'山海'
            ],
            [
            'id' =>2,
            'name' =>'清海'
            ],
            [
            'id' =>3,
            'name' =>'Mr.林'
            ]
        ];
            view::assign('arr' ,$arr);
            
        return view::fetch();
    }
}

在HTML中输入

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    {foreach $arr as $k => $v}<!-- 这里的$K是从0开始计数 -->
        <div>{$k} {$v['name']}</div>
    {/foreach}
</body>
</html>

效果(一一对应)如下:

033e3e521c171220

volist标签

volist标签也是循环标签,它的属性值如下

faeec9abde171554

在thinkphp6中输入

<?php
namespace app\controller;

use think\facade\View;

class Text{
    public function index(){
        $arr = [
        [
            'id' => 1,
            'name' =>'山海'
            ],
            [
            'id' =>2,
            'name' =>'清海'
            ],
            [
            'id' =>3,
            'name' =>'Mr.林'
            ]
        ];
            view::assign('arr' ,$arr);
            
        return view::fetch();
    }
}

在HTML中输入

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    {volist name='arr' id='vv' key='kk' offset='1'}<!-- key属性值是从1开始计数 -->
        <div>{$kk} {$vv['name']}</div>
    {/volist}
</body>
</html>

效果(一一对应)如下:

e1299eab99172024

if标签

if标签是判断标签

在thinkphp6中输入

<?php
namespace app\controller;

use think\facade\View;

class Text{
    public function index(){
        view::assign('status',1);
        view::assign('order_status',4);     
        return view::fetch();
    }
}

在HTML中输入

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    {if $status == 1}
        <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>
    {else/}
        <div>已支付 未发货</div>
    {/if}
</body>
</html>

效果图如下:

a37f4165a0220657

switch标签

switch标签也是判断标签

在thinkphp6中输入

<?php
namespace app\controller;

use think\facade\View;

class Text{
    public function index(){
        view::assign('status',1);
        view::assign('order_status',4);     
        return view::fetch();
    }
}

在HTML中输入

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    {{switch $order_status }
        {case 0}<div>未支付</div>{/case}
        {case 0}<div>已支付 未发货</div>{/case}
        {case 0}<div>已发货 未收货</div>{/case}
        {case 0}<div>已收货 未评论</div>{/case}
        {case 0}<div>已完成</div>{/case}
    {/switch}}
</body>
</html>

效果图如下:

7a64642970221630

include标签

include标签的作用是引入模板标签

首先我们要先在app文件夹中的子文件夹view中创建新文件夹“public”,然后再该文件夹中新建两个文件是“header.html”和“tail.html”

然后将HTML中的前半部分放在文件“header.html”中

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

再将HTML尾部放在文件“tail.html”中

</body>
</html>

然后就可以通过include标签进行省略HTML头尾

{include file='public/header'}
清海
{include file='public/tail'}

效果如下

e3fae21ef2234656

结尾

今天的thinkphp6的知识分享就到此为止啦!敬请期待下一期的thinkphp6的知识分享,感谢大家的支持!

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容