簡單來說一個控制器對應一個視圖,一個方法對應一個模板下面我們直接上圖,

二、給模板賦值
給模板賦值在這里用到了assign()這個函式,assign()函式第一個引數為給這個值自定義名稱,第二個引數則是值
栗子:
public function testview(){ $date=Db::name("goods")->select();//資料庫查詢商品表 回傳一個資料集 $this->assign('date',$date); //把資料集給到date return $this->fetch(); }
那么這樣相應的模板就拿到了date這個資料集了,下面就是這么在模板中使用這個資料集,直接上代碼:
<table>
<tr>
<th>商品ID</th>
<th>商品名稱</th>
<th>商品價格</th>
<th>操作</th>
</tr>
{foreach $date as $value}
<tr>
<th>{$value.id}</th>
<th>{$value.goods_name}</th>
<th>{$value.price}</th>
<th><a href="https://www.cnblogs.com/MingGyGy-Castle/p/#">洗掉</th>
</tr>
{/foreach}
</table>
三、在模板中該怎么注釋
普通的HTML注釋是無法注釋{foreach}中的內容的,因為ThinkPHP會找到"{}"進行處理大括號中的內容,所以在這里我們用特殊的辦法來注釋,用 {/*注釋的內容*/}這樣的方法來注釋下面舉個栗子
<table>
<tr>
<th>商品ID</th>
<th>商品名稱</th>
<th>商品價格</th>
<th>操作</th>
</tr>
<!--多行注釋 用{/*注釋的內容*/}-->
{/*
{foreach $date as $value}
<tr>
<!--單行注釋 用{//注釋的內容}-->
{//<th>{$value.id}</th>}
<th>{$value.goods_name}</th>
<th>{$value.price}</th>
<th><a href="https://www.cnblogs.com/MingGyGy-Castle/p/#">洗掉</th>
</tr>
{/foreach}
*/}
</table>
四、在模板中使用函式
模板中也是可以使用strtoupper、md5等函式的,下面演示具體該怎么使用,
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>test</title>
</head>
<body>
<!--
模版中的使用函式 例如后臺傳入一個jack,使用strtoupper()函式轉成大寫字母
-->
{$name | strtoupper}
<br>
<!--呼叫md5函式給name加密-->
{$name | md5}<br>
<!--多個使用函式 加密后字母大寫-->
{$name | md5 | strtoupper}<br>
<!--使用指定class中的方法 如呼叫VERSION獲取ThinkPHP的版本號-->
{:think\\APP::VERSION}
</body>
</html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/102114.html
標籤:PHP
