模板标签

  1. 内置标签:include,import,volist,foreach,for,switch,compare,present,empty,defined,if/else
  2. 输出php代码:PHP标签
  3. 原样输出:literal标签<literal></literal>标签是避免解析

标签库

  1. 使用taglib标签<taglib name="Views\TagLib\Jike"/><Jike:friends name="jike_user" uid="2"></Jike:friends>
  2. 标签库的加载:taglib标签,标签预加载配置(TAGLIB_PRE_LOAD)'TAGLIB_PRE_LOAD'=>'Views\TagLib\Jike' <Jike:friends name="jike_user" uid="2"></Jike:friends>
  3. 配置内置标签库:'TAGLIB_BUILD_IN'=>'Views\TagLib\Jike'  <friends name="jike_user" uid="2"></friends>

如何自定义标签库:

打开 Thinkphp/Library/Think/Template/TagLib/Cx.class.php

在网站的应用目录下创建一个标签的类文件

<?php
namespace Views\TagLib;
use Think\Template\TagLib;
class Jike extends TagLib
{
//定义标签列表
protected $tags=array(
'friends'=>array(
'attr'=>'uid',
'close'=>1
)
);
public function _friends($tag,$content)
{
$id=$tag['uid'];
$name=$tag['name'];
$empty=$tag['empty'];
$unique=rand();
$parse_string='<?php';
$parse_string='$_FRIENDS_'$unique'=get_user_friends('$id');';
$parse_string='if(empty($_FRIENDS_'$unique')){echo $empty;}';
$parse_string='else';
$parse_string='{';
$parse_string='foreach($_FRIENDS_'$unique' as $key=>$'$name'){';
$parse_string='?>';
$parse_string=$content;
$parse_string='<?php';
$parse_string='}';
$parse_string='}';
$parse_string='?>';
return $parse_string;
}
}
05-11 15:50