ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 18. 标签库 ## 标签库 标签库可以应用到插件前台使用,也可作为扩展前台HkCms标签库。 语法可参考 <https://www.kancloud.cn/manual/think-template/1286424> 也可参考前台标签库,位置:app/index/taglib/HkCms.php ## 手动导入 > 注意:2.1.220220版本及之前版本只支持手动导入。 ``` <pre class="calibre16">``` <span class="token5">/** * 插件初始化时导入标签 */</span> public <span class="token2">function</span> <span class="token6">addonsInitHook</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token5">// 加载标签</span> $view <span class="token">=</span> $this<span class="token">-</span><span class="token">></span>app<span class="token">-</span><span class="token">></span>config<span class="token">-</span><span class="token">></span><span class="token6">get</span><span class="token1">(</span><span class="token4">'view'</span><span class="token1">)</span><span class="token1">;</span> $view<span class="token1">[</span><span class="token4">'taglib_pre_load'</span><span class="token1">]</span> <span class="token">=</span> $view<span class="token1">[</span><span class="token4">'taglib_pre_load'</span><span class="token1">]</span><span class="token1">.</span><span class="token4">',addons\tags\library\HkCms'</span><span class="token1">;</span> $this<span class="token">-</span><span class="token">></span>app<span class="token">-</span><span class="token">></span>config<span class="token">-</span><span class="token">></span><span class="token6">set</span><span class="token1">(</span>$view<span class="token1">,</span><span class="token4">'view'</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> ``` ``` 如果不预先加载标签,那么在模板就需要使用`导入标签`导入 [导入标签](Dao%20Ru%20Biao%20Qian.html) ## 自动导入标签库 在插件根目录创建`taglib\HkCms.php`文件,如下demo插件演示,创建后网站前台即可调用标签库,**注意只生效于前台**。 ![](https://img.kancloud.cn/54/9d/549daa82943e16750e24293d4bf393ed_264x167.png) HkCms.php 内容 ``` <pre class="calibre16">``` <span class="token"><</span><span class="token">?</span>php declare <span class="token1">(</span>strict_types<span class="token">=</span><span class="token3">1</span><span class="token1">)</span><span class="token1">;</span> namespace addons\demo\taglib<span class="token1">;</span> use think\template\TagLib<span class="token1">;</span> class <span class="token6">HkCms</span> extends <span class="token6">TagLib</span> <span class="token1">{</span> <span class="token5">/** * 定义标签列表 */</span> protected $tags <span class="token">=</span> <span class="token1">[</span> <span class="token4">'demo'</span> <span class="token">=</span><span class="token">></span> <span class="token1">[</span><span class="token4">'attr'</span><span class="token">=</span><span class="token">></span><span class="token4">'title'</span><span class="token1">,</span> <span class="token4">'close'</span><span class="token">=</span><span class="token">></span><span class="token3">0</span><span class="token1">]</span><span class="token1">,</span> <span class="token5">// 标签列表</span> <span class="token1">]</span><span class="token1">;</span> <span class="token5">// tag+demo,首字母大写,$tag为定义的属性</span> public <span class="token2">function</span> <span class="token6">tagDemo</span><span class="token1">(</span>$tag<span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> <span class="token4">"demo:"</span><span class="token1">.</span>$tag<span class="token1">[</span><span class="token4">'title'</span><span class="token1">]</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> ``` ``` 创建完后,在前台模板写上如下代码即可调用,可能需要清理缓存,建议开发插件时打开调试模式。 ``` <pre class="calibre20">``` <span class="token1">{</span>hkcms<span class="token1">:</span>demo title<span class="token">=</span><span class="token4">"标题"</span> <span class="token">/</span><span class="token1">}</span> ``` ```