1、新建一个自定义类,common/tag/Custom.php。
namespace app\common\tag;
use \think\template\TagLib;
class Custom extends TagLib
{
protected $tags = array(
'testclose' => array('attr' => 'name,content','close' =>0), //默认为对称标签
'testopen' => array('attr' => 'name'),
);
/**
* 闭合标签
* 注意方法的命名, tag+标签名 标签名大写
*/
public function tagTestclose($tag, $content)
{
$parseStr ="<h1>{$tag['name']}---{$tag['content']}</h1>";
return $parseStr;
}
//对标签
public function tagTestopen($tag,$content){
$parseStr='';
$parseStr.="<h1>{$tag['name']}</h1>";
$parseStr.="<br/>content: {$content}";
return $parseStr;
}
}
2、在配置文件config中配置:
'template' => [
...
// 标签库标签开始标记
'taglib_begin' => '<',
// 标签库标签结束标记
'taglib_end' => '>',
'taglib_build_in'=>'cx,app\common\tag\Custom' //cx为官方的 带加入进去
]
3、在html中调用
<testclose name="测试name" content="测试content"/>
<br>
<testopen name="name 测试">内容content测试</testopen>