Master ThinkTemplate: High‑Performance XML‑Based PHP Template Engine
ThinkTemplate is a high‑performance, XML‑based compiled PHP template engine originally built for ThinkPHP, offering customizable delimiters, native PHP support, file inclusion, multi‑level nesting, caching, variable defaults, security controls, and flexible tag types, with detailed installation and usage examples for web frameworks.
Overview
ThinkTemplate is an XML‑based compiled template engine with no external dependencies. It originated as the built‑in engine of ThinkPHP and can now be used as a standalone component.
Key Features
Custom tag delimiters – you can set different start/end delimiters for normal tags ( tpl_begin / tpl_end) and tag‑library tags ( taglib_begin / taglib_end).
Native PHP support – raw PHP code can be embedded directly in templates.
File inclusion – {include file="..."} or <include file="..."> to import other template files.
Multi‑level tag nesting – tags can be nested arbitrarily, enabling complex page structures.
Layout templates – a layout file can define a common skeleton and child templates inject content via block tags.
Compilation & caching – each template is compiled to a PHP file stored in the runtime cache directory. The cache file is regenerated automatically when the source template changes; disabling the cache forces recompilation on every render.
System variable output – built‑in variables (e.g., request data) can be printed without explicit assignment.
Multi‑dimensional array access – dot notation ( {$data.user.name}) or array syntax ( {$data['user']['name']}) works.
Default values – {$var|default='N/A'} prevents undefined‑variable notices.
HTML whitespace optimization – optional removal of redundant spaces and line breaks in the final output.
Variable modifiers – filters such as upper, lower, date, or custom callbacks can be chained.
PHP function restriction – the configuration key php_functions can whitelist or blacklist functions for security.
Tag‑library extensions – developers can create custom XML‑style tags by registering a tag library class.
Tag Types
ThinkTemplate defines two tag families:
Normal tags – simple delimiters (
{ }) used for variable output, filters and comments.
XML tags – also called tag‑library tags, using delimiters (
{ }by default) but configurable separately. They provide control structures, loops, includes and can be extended.
Normal Tags
Examples: {$name} – prints the variable name. {$vo.name} – accesses a nested property. {$vo['name']|strtoupper} – applies the strtoupper filter.
Delimiters must be adjacent to the tag; whitespace breaks parsing (e.g., { $name} is treated as plain text). Custom delimiters are set in the configuration array, for example:
'tpl_begin' => '<{',
'tpl_end' => '}>',XML Tags
XML tags support logic, loops and includes. The default delimiters are the same as normal tags but can be changed:
'taglib_begin' => '<',
'taglib_end' => '>',Conditional example using default delimiters:
{eq name="name" value="value"}
相等
{else/}
不相等
{/eq}When custom delimiters are applied the same logic becomes:
<eq name="name" value="value">
相等
<else/>
不相等
</eq>Installation
Install the package via Composer:
composer require topthink/think-templateFor the Webman framework, configure the view handler in config/view.php:
<?php
use support\view\ThinkPHP;
return [
'handler' => ThinkPHP::class,
];Controller example ( app/controller/UserController.php) that returns a view:
<?php
namespace app\controller;
use support\Request;
class UserController
{
public function hello(Request $request)
{
return view('user/hello', ['name' => '开源技术小栈']);
}
}Corresponding view file ( app/view/user/hello.html) using a variable:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>webman</title>
</head>
<body>
hello {$name}
</body>
</html>Further documentation is available at https://www.kancloud.cn/manual/think-template
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
