thinkphp5.1學習過程九——模板變量與經常使用標籤

<?php


namespace app\index\controller;

use think\Controller;
use think\facade\View;
use app\index\model\Blog;
class Demo7 extends Controller
{
    public function test1()
    {
        //直接將內容輸出到頁面,不經過模板
        $content = '<h2>海之網</h2>';
//        return $this->display($content);
//        return $this->view->display($content);
        return View::display($content);//靜態代理
    }

    //使用視圖將數據輸出:fetch()
    public function test2()
    {
        //模板變量賦值:assign()
        //1.普通變量
        $this->view->assign('name', '大海');
        $this->view->assign('age', '18');
        //批量賦值
        $this->view->assign([
            'sex' => '男',
            'salary' => 666
        ]);
        //2.array
        $this->view->assign('goods', [
            'id' => 1,
            'name' => '手機',
            'model' => 'meto',
            'price' => 999
        ]);
        //3.object
        $obj = new \stdClass();
        $obj->course = 'php';
        $obj->lecture = 'yht';
        $this->view->assign('info', $obj);
        //4.const
        define('SITE_NAME', 'PHP學習');


        //在模板中輸出數據
        //模板默認的目錄位於當前模塊的view目錄,模板文件默認位於當前控制器目錄中
        return $this->view->fetch();
    }

    public function test3()
    {
        $data=Blog::all();
        $this->view->assign('data',$data);
        return $this->view->fetch();
    }
    public function test4()
    {
        //獲取分頁要調用查詢類中的paginate(num)方法
        //第頁顯示3條
        $data=Blog::paginate(3);
        $this->view->assign('data',$data);
        return $this->view->fetch();
    }
}
<?php


namespace app\index\model;

use think\Model;
class Blog extends Model
{

}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test2</title>
</head>
<body>
{$name}<br>
{$age}<br>
{$sex}<br>
{//輸出數組}<br>
{$goods.id}<br>
{$goods['name']}<br>
{//輸出對象}<br>
{$info->course}<br>
{//輸出常量}<br>
{$Think.const.SITE_NAME}
{//輸出系統常量}<br>
{$Think.const.PHP_VERSION}<br>
{$Think.const.PHP_OS}<br>
{//輸出系統變量$_SERVER['PHP_SELF']}<br>
{$Think.server.php_self}<br>
{$Think.server.session.id}<br>
{$Think.server.get.name}<br>
{$Think.server.post.name}<br>
{//輸出配置項}<br>
{$Think.config.database.hostname}<br>
{//輸出請求變量}<br>
{$Request.get.name}<br>
{$Request.param.name}<br>
{$Request.path}<br>
{$Request.root.true}<br>
</body>
</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.min.js."></script>
    <script src="/static/js/bootstrap.min.js."></script>
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-2"></div>
        <div class="col-md-8">
            <h2 class="text-center">新聞內容</h2>
            <table class="table table-bordered table-hover">
                <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.title}</td>
                    <td>{$list.content}</td>
                    <td>{$list.recommend}</td>
                </tr>
                {/volist}
            </table>
        </div>
        <div class="col-md-2"></div>
    </div>
</div>
</body>
</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.min.js."></script>
    <script src="/static/js/bootstrap.min.js."></script>
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-2"></div>
        <div class="col-md-8">
            <h2 class="text-center">新聞內容</h2>
            <table class="table table-bordered table-hover">
                <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.title}</td>
                    <td>{$list.content}</td>
                    <td>
                        {if $list.recommend == 1}
                            <span style="color:green">普通</span>
                        {else/}
                            <span style="color:red">推薦</span>
                        {/if}
                    </td>
                </tr>
                {/volist}
            </table>
        </div>
        <div class="col-md-2"></div>
        <div class="text-center">
            {$data|raw}
        </div>

    </div>
</div>
</body>
</html>
相關文章
相關標籤/搜索