Blogger Information
Blog 61
fans 0
comment 0
visits 62938
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
tp5.1模块变量与常用标签
Pengsir
Original
752 people have browsed it

Demo7中test4代码:

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/22 0022
 * Time: 17:56
 */

namespace app\index\controller;
use think\Controller;
use think\facade\View;
use app\index\model;


class Demo7 extends Controller
{
    //1.直接将内容输出到页面,不通过模板
    public function test1(){
        $content = '<h3 style="color: red">php中文网欢迎你</h3>';
//        return $this->display($content);
        return $this->view->display($content);//推荐这种方式
        return View::display($content);//静态代理
    }
//   2.使用视图将数据输出:fetch()
    public function test2(){
//        模板变量赋值:assign()
//        1.普通变量
        $this->view->assign('name','peng');
        $this->view->assign('age',18);
        //批量赋值
        $this->view->assign([
            'sex'=>'男',
            'salary'=>666
        ]);
//        2.array 数组
        $this->view->assign('goods',[
            'id'=>1,
            'name'=>'手机',
            'model'=>'ipone6',
            'price'=>5000
        ]);
//        3.object 对象
        $obj=new \stdClass();
        $obj->course='PHP';
        $obj->lecture ='peng';
        $this->view->assign('info',$obj);
//        4.const 常量
        define('SITE_NAME','java');
//        在模板中输出数据
//        模板默认的目录位于当前模块的view目录,模板文件默认位于以当前控制器命名的目录中
            return $this->view->fetch();
    }

    public function test3(){
        $data=model\Student::all();
        $this->view->assign('data',$data);
        return View::fetch();
    }

    public function test4(){
        //获取分页要调用查询类中的paginate(num)方法
        //每页显示5条
        $data = model\Student::paginate(5);
        $this->view->assign('data',$data);
        return View::fetch();
    }
}

view模块下test4.html代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{$title|default='默认标题'}</title>
    <link rel="stylesheet" href="/static/css/bootstrap.min.css">
    <script src="/static/js/jquery-3.2.1.min.js"></script>
    <script src="/static/js/bootstrap.min.js"></script>
</head>
<body>
<div class="contain">
    <div class="row">
        <div class="com-md-2"></div>
        <div class="com-md-8">
            <h2 class="text-center">学生信息</h2>
            <table class="table table-default table-bordered table-hover text-center">
                <tr class="bg-primary">
                    <td>ID</td>
                    <td>姓名</td>
                    <td>邮箱</td>
                    <td>课程</td>
                    <td>成绩</td>
                </tr>
                {volist name='data' id='list'}
                <tr>
                    <td>{$list.id}</td>
                    <td>{$list.name}</td>
                    <td>{$list.email}</td>
                    <td>{$list.course}</td>
                    <td>
                        {if $list.grade <60}
                        <span style="color: red">不及格</span>>
                            {else\}
                        <span style="color: green">及格</span>>
                                {/if}
                    </td>
                </tr>
                {/volist}
            </table>
        </div>
        <div class="com-md-2"></div>
        <div class="text-center">
            {$data|raw}
        </div>

    </div>
</div>
</body>
</html>

模型model下的Student.php

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/22 0022
 * Time: 17:09
 */
namespace app\index\model;

use think\Model;

class Student extends Model
{
//    protected $table = '对应一张数据库表名';//这里可以设置绑定数据库的那张表,不写就是我们这里设的Student表
}

运行结果图:

tp5模块与常用标签练习.png

tp5对应代码图:view下的test4.pngDemo7.png

对应数据库Adminer下的student表.png

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post