tp5扩展标签库

#1、创建自定义标签库文件Test.php

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace think\template\taglib;

use think\template\TagLib;

/**
* CX标签库解析类
* @category Think
* @package Think
* @subpackage Driver.Taglib
* @author liu21st <liu21st@gmail.com>
*/
class Test extends Taglib
{
//////////
// 标签定义 //
// attr:标签属性
// alias:标签别名
// close:是否为闭合标签(1),非闭合(0)
//////////
protected $tags = [
'mytest'=>['attr' => 'color,border','alias'=>'yourtest','close'=>1]
];

/**
* mytest标签解析
* 格式:
* {mytest}echo $name{/mytest}
* @access public
* @param array $tag 标签属性
* @param string $content 标签内容
* @return string
*/

public function tagmytest($tag,$content)
{
$color="color:{$tag['color']};";
$border="border:{$tag['border']}px solid #000;";
$css=$color.$border;
return "<div style='{$css}'>".$content."</div>";
}
}

 

#视图中调用

<!--
 加载扩展标签类
 -->
{taglib name="test" /}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!--
     * 调用扩展标签
     * 定义标签时有属性,则调用的时候必须设置属性值
     -->
    {test:mytest color='red' border='10'}川科院{/test:mytest}
</body>
</html>

 

#预加载标签

在app\config.php中设置

    'template'               => [
        //默认加载标签
        'taglib_build_in'=>'test'
    
        //预加载标签,如果设置了默认,则不需要再设置预加载
        'taglib_pre_load'=>'test',
    ],

 

 

posted @ 2023-02-08 10:46  哆啦阿梦  阅读(39)  评论(0编辑  收藏  举报