視圖和模板
[TOC]
控制器(在php文件)使用模板:
VIew模板輸出:
$this->assign('name','value'); //定義view里的變量$name的值
return $this->fetch('index/index');//參數(shù)為空時(shí)默認(rèn)當(dāng)前控制器對(duì)應(yīng)的view
相應(yīng)的html文件下使用name變量:{$name}
<span style='color:#fb7299'>eg1:展示用戶數(shù)據(jù)</span>
輸出用戶列表
控制器中:<a name='con'></a>
$list = User::all(); //返回User對(duì)象數(shù)組
$this->assign('list',$list);
$this->assign('count',count($list));
return $this->fetch('index/index');//參數(shù)為空時(shí)默認(rèn)當(dāng)前控制器對(duì)應(yīng)的view
模板視圖:
<h1>用戶列表 {$count}</h1>
{volist name="list" id="user"}
<!--name="list"將$list數(shù)組傳了過來,$user是每次遍歷的數(shù)組的元素-->
<div class="container" style="border: 1px black dashed">
<ul>
<li>id:{$user.id}</li>
<li>名字:{$user.name}</li>
<li>生日:{$user.birthday}</li>
<li>郵箱:{$user.email}</li>
</ul>
</div>
{/volist}
渲染模板是會(huì)在runtime的temp生成臨時(shí)文件
對(duì)應(yīng)生成的臨時(shí)文件片段:
<?php if(is_array($list) || $list instanceof \think\Collection || $list instanceof \think\Paginator): $i = 0; $__LIST__ = $list;if( count($__LIST__)==0 ) : echo "" ;else: foreach($__LIST__ as $key=>$user): $mod = ($i % 2 );++$i;?>
<!--name="list"將$list數(shù)組傳了過來,$user是每次遍歷的數(shù)組的元素-->
<div class="container" style="border: 1px black dashed">
<ul>
<li>id:<?php echo $user['id']; ?></li>
<li>名字:<?php echo $user['name']; ?></li>
<li>生日:<?php echo $user['birthday']; ?></li>
<li>郵箱:<?php echo $user['email']; ?></li>
</ul>
</div>
<?php endforeach; endif; else: echo "" ;endif; ?>
<span style='color:#fb7299'>eg2:分頁(yè)展示用戶數(shù)據(jù)</span>
在以上的 控制器代碼中,將第一行改為:
$list = User::paginate(2); //每頁(yè)展示兩條數(shù)據(jù)
view在{/volist}末尾加:
{$list->render()}
會(huì)在下面生成頁(yè)面跳轉(zhuǎn)底部導(dǎo)航
? 另一種方式:$page = ?$list->render();
臨時(shí)文件:
<?php echo $list->render(); ?>
url會(huì)在后面自動(dòng)加上?page=1 ?page=2...
最后生成的html元素:
<ul class="pagination">
<li class="disabled"><span>?</span></li>
<li class="active"><span>1</span></li>
<li><a href="/tp5_02/public/index?page=2">2</a></li>
<li><a href="/tp5_02/public/index?page=2">?</a></li>
</ul>
配置文件中對(duì)于分頁(yè)的配置在代碼最下面:
//分頁(yè)配置
'paginate' => [
'type' => 'bootstrap',
'var_page' => 'page',
'list_rows' => 15,
其實(shí)的type是指分頁(yè)的樣式文件在這個(gè)文件夾下
"D:\code\phpSstudy\WWW\tp5_02\thinkphp\library\think\paginator\driver\Bootstrap.php"
官方關(guān)于分頁(yè)的文檔:https://www.kancloud.cn/manual/thinkphp5/154294
一個(gè)分頁(yè)樣式:http://www.thinkphp.cn/code/3000.html
<div style='border: 1px black dashed;'>
1.假設(shè)這里有3數(shù)據(jù),當(dāng)前展示第一頁(yè)時(shí),paginate獲取的是前兩條數(shù)據(jù),因此count為2,頁(yè)面就顯示:用戶列表 2
當(dāng)前展示第二頁(yè)時(shí)頁(yè)面就顯示:用戶列表 1
2.list返回的是配置中type對(duì)應(yīng)的thinkphp\library\think\paginator\driver\Bootstrap.php的類
</div>
公共模板:
1.
加載頭部模板和尾部模板
{include file="index/header"/}
//html+php代碼
{include file="index/footer"/}
生成的臨時(shí)文件就是將文件代碼拼切起來在第一行寫上:
<?php if (!defined('THINK_PATH')) exit(); /*a:3:{s:78:"D:\code\phpSstudy\WWW\tp5_02\public/../application/index\view\index\index.html";i:1555132297;s:69:"D:\code\phpSstudy\WWW\tp5_02\application\index\view\Index\header.html";i:1555132224;s:69:"D:\code\phpSstudy\WWW\tp5_02\application\index\view\Index\footer.html";i:1555132265;}*/ ?>
模板路徑配置:將默認(rèn)的路徑'../application/index/view/'改為'../template/home/'
// 模板路徑
//'view_path' => '',
'view_path' => '../template/home/',
PS:其下(這里的是home)的文件夾首字母一般大寫
模板傳參:
? 模板變量聲明[title]
? 引用:
{include file="index/header" title="maid"/}
2.使用布局
(1).直接使用布局
在view下直接加一個(gè)html文件:
{include file="index/header"/}
{__CONTENT__}
{include file="index/footer"/}
使用布局:
{layout name="layout"} //name是html文件名
(2).全局配置,默認(rèn)加載布局文件
//沒有選項(xiàng)就添加
'layout_on' => true,
'layout_name' => 'layout',
'layout_item' => '{__CONTENT__}',
這種情況下某個(gè)頁(yè)面排除默認(rèn)布局:
{__NOLAYOUT__}
標(biāo)簽定制
// 標(biāo)簽庫(kù)標(biāo)簽開始標(biāo)記
'taglib_begin' => '{',
// 標(biāo)簽庫(kù)標(biāo)簽結(jié)束標(biāo)記
'taglib_end' => '}',
如果換成“<”和">",那么模板引擎需要這么寫:
<volist name="list" id="user">
<!--todo-->
</volist>
一般不建議修改
輸出替換
在控制器中模板解析之前:
$this->view->replace(['__PUBLIC__' => '/static'])//解析時(shí)將模板中的'__PUBLIC__'替換成'/static'
return $this->fetch();
加到控制器中可以自動(dòng)在本控制器的每個(gè)方法使用:
public function __construct()
{
parent::__construct();
$this->view->replace(['用戶列表' => 'maid']);
}