首页 > 代码库 > taglib
taglib
thinkphp中 taglib标签应用
原文出处:http://blog.csdn.net/a11085013/article/details/38172653
1、配置文件中加上:
‘APP_AUTOLOAD_PATH‘ => ‘@.TagLib‘,
2、在项目的/Lib/下新建 TagLib文件夹 复制TagLibHtml.class.php到该目录下面
也可以自己定义格式Tag大驼峰命名.class.php
实例:TagLibCms.class.php代码下面;
3、模版
<tagLib name="html" />
<html:imageBtn name="add" value="http://www.mamicode.com/新增" />
或
<cms:article name="content" limit="10" cid="1">
<h1><a href="http://www.mamicode.com/{$article.ename}/{$article.id}.html">hhh</a></h1>
</cms:article>
TagLibCms.class.php的代码:
[code]
<?php
import("TagLib");
class TagLibCms extends TagLib{
private $CategoryArr = array();
private $SubString = ‘‘;
protected $tags = array(
‘article‘=>array(‘attr‘=>‘name,field,limit,order,cid,where,sql,key,mod‘,‘level‘=>3),
);
public function _article($attr,$content){
$tag = $this->parseXmlAttr($attr,‘article‘);
$result = !empty($tag[‘result‘])?$tag[‘result‘]:‘article‘; //定义数据查询的结果存放变量
$key = !empty($tag[‘key‘])?$tag[‘key‘]:‘i‘;
$mod = isset($tag[‘mod‘])?$tag[‘mod‘]:‘2‘;
//print_r($tag[‘name‘]);die();
if ($tag[‘name‘]){
$sql = "M(‘{$tag[‘name‘]}‘)->";
$sql .= ($tag[‘field‘])?"field({$tag[‘field‘]})->":‘‘;
$sql .= ($tag[‘order‘])?"order({$tag[‘order‘]})->":‘‘;
if($tag[‘cid‘]){
$cidarr = explode(‘,‘,$tag[‘cid‘]);
foreach($cidarr AS $k=>$v){
$this->getSubCategory($v);
}
$cidstr = implode(",",$this->SubString);
$tag[‘where‘] = "cid IN(".$cidstr.")";
$sql .= ($tag[‘where‘])?"where(\"{$tag[‘where‘]}\")->":‘‘;
}else{
$sql .= ($tag[‘where‘])?"where(\"{$tag[‘where‘]}\")->":‘‘; //被重新处理过了
}
$sql .= "select()";
echo $sql;die();
}
//下面拼接输出语句
$parsestr = ‘<?php $_result=‘.$sql.‘; if ($_result): $‘.$key.‘=0;‘;
$parsestr .= ‘foreach($_result as $key=>$‘.$result.‘):‘;
$parsestr .= ‘++$‘.$key.‘;$mod = ($‘.$key.‘ % ‘.$mod.‘ );?>‘;
$parsestr .= $content;//解析在article标签中的内容
$parsestr .= ‘<?php endforeach; endif;?>‘;
return $parsestr;
}
}
[/code]
taglib