- 模板引擎支持普通标签和XML标签方式两种标签定义,分别用于不同的目的
标签类型 | 描述 |
---|---|
普通标签 | 主要用于输出变量,函数过滤和做一些基本的运算操作 |
XML标签 | 也称为标签库标签,主要完成一些逻辑判断,控制和循环输出,并且可扩展 |
一、运算符
运算符 | 示例 |
---|---|
+ | {$ a+$b} |
- | {$ a-$b} |
* | {$ a*$b} |
/ | {$ a/$b} |
% | {$ a%$b} |
++ | {$ a++}或{++$a} |
– | {$ a–}或{–$a} |
综合运算 | {$ a+$ b*10+$c} |
三元运算符 | {$ a==$b?‘yes’:‘No’} |
Controller文件
<?php
namespace app\controller;
use app\BaseController;
use think\facade\View; //引用门面类
class Index extends BaseController {
public function index() { return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V' . \think\facade\App::version() . '<br/><span style="font-size:30px;">14载初心不改 - 你值得信赖的PHP框架</span></p><span style="font-size:25px;">[ V6.0 版本由 <a href="https://www.yisu.com/" target="yisu">亿速云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ee9b1aa918103c4fc"></think>';
}
public function hello($name = 'ThinkPHP6') {
return 'hello,' . $name;
}
public function test() {
return view::fetch();
}
public function login() {
return view::fetch('index/login');
}
//模板变量
public function variable(){
//使用助手函数进行赋值
return view('test',[
'name'=>'moonloon',
'qq'=>'1809620325'
]);
}
public function Operator(){
view::assign('a',520);
view::assign('b',13);
return view::fetch();
}
}
View文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>{$a+$b}</div>
<div>{$a-$b}</div>
<div>{$a*$b}</div>
<div>{$a/$b}</div>
<div>{$a%$b}</div>
<div>{$a++}</div>
<div>{$a--}</div>
<div>{$a+$b*10}</div>
<div>{$a===$b?'yes':'no'}</div>
</body>
</html>
展示结果
二:模板函数
方法 | 描述 |
---|---|
date | 日期格式化(支持各种时间类型) |
upper | 转换为大写 |
lower | 转换为小写 |
first | 输出数组的第一个元素 |
last | 输出数组的最后一个元素 |
default | 默认值 |
raw | 不使用(默认)转义 |
md5 | md5加密 |
substr | 截取字符串 |
- 可以多函数调用
Controller文件
<?php
namespace app\controller;
use app\BaseController;
use think\facade\View; //引用门面类
class Index extends BaseController {
public function index(){ return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V' . \think\facade\App::version() . '<br/><span style="font-size:30px;">14载初心不改 - 你值得信赖的PHP框架</span></p><span style="font-size:25px;">[ V6.0 版本由 <a href="https://www.yisu.com/" target="yisu">亿速云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ee9b1aa918103c4fc"></think>';
}
public function hello($name = 'ThinkPHP6'){
return 'hello,' . $name;
}
public function test(){
return view::fetch();
}
public function login(){
return view::fetch('index/login');
}
//模板变量
public function variable(){
//使用助手函数进行赋值
return view('test',[
'name'=>'moonloon',
'qq'=>'1809620325'
]);
}
//运算符
public function Operator(){
view::assign('a',520);
view::assign('b',13);
return view::fetch();
}
//模板函数
public function mouldefx(){
view::assign('time',1604649103);
view::assign('num',520.1314);
view::assign('str','Iloveyou');
view::assign('arr',[
'小黄',
'小虾',
'小红',
]);
return view::fetch();
}
}
View文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>{$time|date='y-m-d H:i:s'}</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>
<div></div>
</body>
</html>
展示结果
模板支持注释功能,该注释文字在最终页不会显示,仅供模板制作人员参考和识别
单行注释 格式:
{/* 注释内容 */} 或 {// 注释内容}
注意{和注释标记之间不能有空格
多行注释 格式:
{/*这是模板 注释内容 */}
注意:模板注释在生成编译缓存文件后会自动删除,这一点和Html的注释不同
原样输出
可以使用literal标签来防止模板标签被解析,例如:
{literal} hello,{$name} {/literal}
循环标签
- foreach 标签的用法和PHP语法非常接近,用于循环输出数组或者对象的属性
Controller文件
<?php
namespace app\controller;
use app\BaseController;
use think\facade\View; //引用门面类
class Index extends BaseController { public function index() { return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V' . \think\facade\App::version() . '<br/><span style="font-size:30px;">14载初心不改 - 你值得信赖的PHP框架</span></p><span style="font-size:25px;">[ V6.0 版本由 <a href="https://www.yisu.com/" target="yisu">亿速云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ee9b1aa918103c4fc"></think>'; }
public function hello($name = 'ThinkPHP6'){
return 'hello,' . $name;
}
public function test(){
return view::fetch();
}
public function login(){
return view::fetch('index/login');
}
//模板变量
public function variable(){
//使用助手函数进行赋值
return view('test',[
'name'=>'moonloon',
'qq'=>'1809620325'
]);
}
//运算符
public function Operator(){
view::assign('a',520);
view::assign('b',13);
return view::fetch();
}
//模板函数
public function mouldefx(){
view::assign('time',1604649103);
view::assign('num',520.1314);
view::assign('str','Iloveyou');
view::assign('arr',[
'小黄',
'小虾',
'小红',
]);
return view::fetch();
}
//循环标签
public function looplabel(){
$arr=[
['id'=>'1' , 'name'=>'小谭' , ],
['id'=>'2' ,'name'=>'小秋' ,],
['id'=>'3' ,'name'=>'小周' ,]
];
view::assign('arr',$arr);
return view::fetch();
}
}
view文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{foreach $arr as $k=>$v}
<div>
<span>id:{$v['id']}</span>
<span>姓名:{$v['name']}</span>
</div>
{/foreach}
</body>
</html>
展示结果
volist循环标签
- 二维数组的结果输出
- name模板赋值的变量名称
- id当前的循环变量,可以随意起名
- key下标,从1开始,默认变量;
- offset开始行数
- length获取行数
- empty如果数据为空,显示此文字
view文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{foreach $arr as $k=>$v}
<div>
<span>id:{$v['id']}</span>
<span>姓名:{$v['name']}</span>
</div>
{/foreach}
{volist name='arr' id='v' key='k' offest='1' length='2'}
<div>
<span>id1:{$v['id']}</span>
<span>姓名1:{$v['name']}</span>
</div>
{/volist}
</body>
</html>
展示结果
for循环标签
for标签的用法和PHP语法非常接近语法
{for start='开始值' end='结束值' comparison="" step="步进值" name="循环变量名"}
{/for}
开始值,结束值,步进值和循环变量都可以支持变量,开始值和结束值是必须,其他是可选,comparison的默认值是It,name的默认值是i,步进值的默认值是1,举例如下:
{for start="1" end="100"} {$i} {/for}
解析后的代码就是
for($i=1;$i<100;$i++){ echo $i; }
if判断标签
if标签的用法和PHP语法非常接近,用于条件判断‘
Controller文件
<?php
namespace app\controller;
use app\BaseController;
use think\facade\View; //引用门面类
class Index extends BaseController {
public function index()
{ return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V' . \think\facade\App::version() . '<br/><span style="font-size:30px;">14载初心不改 - 你值得信赖的PHP框架</span></p><span style="font-size:25px;">[ V6.0 版本由 <a href="https://www.yisu.com/" target="yisu">亿速云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ee9b1aa918103c4fc"></think>';
}
public function hello($name = 'ThinkPHP6'){
return 'hello,' . $name;
}
public function test(){
return view::fetch();
}
public function login(){
return view::fetch('index/login');
}
//模板变量
public function variable(){
//使用助手函数进行赋值
return view('test',[
'name'=>'moonloon',
'qq'=>'1809620325'
]);
}
//运算符
public function Operator(){
view::assign('a',520);
view::assign('b',13);
return view::fetch();
}
//模板函数
public function mouldefx(){
view::assign('time',1604649103);
view::assign('num',520.1314);
view::assign('str','Iloveyou');
view::assign('arr',[
'小黄',
'小虾',
'小红',
]);
return view::fetch();
}
//if标签
public function looplabel(){
view::assign('status',1);
view::assign('week',3);
return view::fetch();
}
}
view文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{if $status==1}
<div>开启</div>
{/if}
{if $status==0}
<div>关闭</div>
{else/}
<div>开启</div>
{/if}
{if $week == 0}
<div>星期天</div>
{elseif $week ==1/}
<div>星期一</div>
{elseif $week ==2/}
<div>星期二</div>
{elseif $week ==3/}
<div>星期三</div>
{elseif $week ==4/}
<div>星期四</div>
{elseif $week ==5/}
<div>星期五</div>
{elseif $week ==6/}
<div>星期六</div>
{/if}
</body>
</html>
展示结果
switch标签
switch标签的用法和PHP语法非常接近,用于条件判断
view文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{switch $week}
{case 0}<div>星期日</div>{/case}
{case 1}<div>星期一</div>{/case}
{case 2}<div>星期二</div>/case}
{case 3}<div>星期三</div>{/case}
{case 4}<div>星期四</div>{/case}
{case 5}<div>星期五</div>{/case}
{case 6}<div>星期六</div>{/case}
{/switch}
</body>
</html>
展示结果
其他标签(参考tp5手册)
- 条件标签
标签 | 描述 |
---|---|
in | 判断变量是否存在某些值 |
notin | 判断变量是否不存在某些值 |
between | 判断变量是否存在某些范围值 |
notbetween | 判断变量是否不存在某些范围值 |
present | 判断某个变量是否 已定义 |
notpresent | 判断某个变量是否 未定义 |
empty | 判断某个变量是否为空 |
notempty | 判断某个变量是否不为空 |
defined | 判断某个常量是否已定义 |
notdefined | 判断某个变量是否未定义 |
- 比较标签
标签 | 描述 |
---|---|
eq或equal | 等于 |
neq或notequal | 不等于 |
gt | 大于 |
It | 小于 |
elt | 小于等于 |
heq | 恒等于 |
nheq | 不恒等于 |
使用原生PHP语句
{php} echo "观星";{/php}
展示结果