thinkphp
ThinkPHP 是一个免费开源的,快速、简单的面向对象的轻量级 PHP 开发框架,是为了敏捷 WEB 应用开发和简化企业应用开发而诞生的 github (opens new window) doc-v8 (opens new window)
# 模板引擎
以下是一些常用的 ThinkPHP 模板引擎代码示例:
# 基本用法
- 输出变量:
<p>{$name}</p>
1
- URL 生成:
<a href="{:url('checkcache')}">Check Cache</a>
1
- 包含模板:
{include file="public/header"}
1
# 条件判断
- 简单的 if 语句:
{if condition="$user.id eq 1"}
<p>Welcome, Admin!</p>
{else /}
<p>Welcome, User!</p>
{/if}
1
2
3
4
5
6
2
3
4
5
6
- 复杂条件:
{if condition="$user.status eq 'active' and $user.role eq 'admin'"}
<p>Admin Panel</p>
{elseif condition="$user.status eq 'active'" /}
<p>User Dashboard</p>
{else /}
<p>Please contact support.</p>
{/if}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 循环
- 遍历数组:
{volist name="list" id="item"}
<p>{$item.name}</p>
{/volist}
1
2
3
2
3
- 遍历关联数组:
{volist name="user.roles" id="role"}
<p>{$role}</p>
{/volist}
1
2
3
2
3
# 模板包含和继承
- 包含其他模板文件:
{include file="public/header"}
1
- 模板继承:
<html>
<head>
<title>{block name="title"}Default Title{/block}</title>
</head>
<body>
{block name="content"}Default Content{/block}
</body>
</html>
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
{extend name="base" /}
{block name="title"}Homepage Title{/block}
{block name="content"}
<p>This is the homepage content.</p>
{/block}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 示例
以下是一个完整的示例,演示了如何使用 ThinkPHP 模板引擎生成一个简单的网页:
控制器:public function index()
{
$this->assign('name', 'ThinkPHP');
$this->assign('user', ['id' => 1, 'name' => 'Admin', 'status' => 'active', 'role' => 'admin']);
return $this->fetch();
}
1
2
3
4
5
6
2
3
4
5
6
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<title>{$name}</title>
</head>
<body>
<h1>Welcome, {$user.name}!</h1>
{if condition="$user.status eq 'active' and $user.role eq 'admin'"}
<p>Admin Panel</p>
{elseif condition="$user.status eq 'active'" /}
<p>User Dashboard</p>
{else /}
<p>Please contact support.</p>
{/if}
<a href="{:url('checkcache')}">Check Cache</a>
{include file="public/footer"}
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
通过这种方式,ThinkPHP 的模板引擎使得前后端的分离更加清晰,同时也使得模板代码的维护和管理更加方便
# 模版解析源码
以下摘自 Template#parse 方法
public function parse(&$content)
{
// 内容为空不解析
if (empty($content)) {
return;
}
// 替换literal标签内容
$this->parseLiteral($content);
// 解析继承
$this->parseExtend($content);
// 解析布局
$this->parseLayout($content);
// 检查include语法
$this->parseInclude($content);
// 替换包含文件中literal标签内容
$this->parseLiteral($content);
// 检查PHP语法
$this->parsePhp($content);
// 获取需要引入的标签库列表
// 标签库只需要定义一次,允许引入多个一次
// 一般放在文件的最前面
// 格式:<taglib name="html,mytag..." />
// 当TAGLIB_LOAD配置为true时才会进行检测
if ($this->config['taglib_load']) {
$tagLibs = $this->getIncludeTagLib($content);
if (!empty($tagLibs)) {
// 对导入的TagLib进行解析
foreach ($tagLibs as $tagLibName) {
$this->parseTagLib($tagLibName, $content);
}
}
}
// 预先加载的标签库 无需在每个模板中使用taglib标签加载 但必须使用标签库XML前缀
if ($this->config['taglib_pre_load']) {
$tagLibs = explode(',', $this->config['taglib_pre_load']);
foreach ($tagLibs as $tag) {
$this->parseTagLib($tag, $content);
}
}
// 内置标签库 无需使用taglib标签导入就可以使用 并且不需使用标签库XML前缀
$tagLibs = explode(',', $this->config['taglib_build_in']);
foreach ($tagLibs as $tag) {
$this->parseTagLib($tag, $content, true);
}
// 解析普通模板标签 {$tagName}
$this->parseTag($content);
// 还原被替换的Literal标签
$this->parseLiteral($content, true);
return;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 相关源码文件
- 'app\common\taglib\Maccms', 可通过 taglib_pre_load 配置
# link
上次更新: 2025/07/17, 17:31:43