自定义模板函数通过在app/common.php定义并注册到config/template.php,如format_time;自定义标签需创建继承TagLib的类并注册标签库,如Test标签输出Hello, ThinkPHP!。

ThinkPHP 模板引擎支持自定义标签和函数,方便开发者扩展功能。以下以 ThinkPHP 6 为例,介绍如何自定义模板标签和函数。
模板函数在模板中直接调用,常用于格式化数据、输出公共内容等。
步骤:
function format_time($time) {<br> return date('Y-m-d H:i:s', $time);<br>}立即学习“PHP免费学习笔记(深入)”;
'tpl_func' => [<br> 'format_time' => 'app\common\format_time'<br>]
{$create_time|format_time}模板标签用于实现更复杂的逻辑控制,比如循环、条件判断或调用模型数据。
步骤:
namespace app\taglib;<br><br>use think\template\TagLib;<br><br>class Test extends TagLib<br>{<br> protected $tags = [<br> 'hello' => ['close' => 1]<br> ];<br><br> public function tagHello($tag, $content)<br> {<br> $name = $tag['name'] ?? 'World';<br> return '<?php echo "Hello, ' . $name . '!"; ?>';<br> }<br>}'taglib_pre_load' => 'app\taglib\Test'
<test:hello name="ThinkPHP" />
以上就是thinkphp模板引擎的标签和函数如何自定义的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号