-
{dede:arclist flag='h' limit='1,4'}
-
<div class='d1arc'><a href="[field:arcurl/]">[field:title/]</a></div>
-
{/dede:arclist}
复制代码
然而在TP中也提供了一些标签,但是在大量展示页面时需要制作太多的控制器,而在控制器中要大量的调用重复数据,所以为了更好的简约代码提高效率,在TP中也可以实现自定义标签。最终效果:该方法可以在标签中使用函数
-
<shoplist name=''>
-
<li>
-
<span>商家ID:[tag:shop_id]</span>
-
<span>商家分类:[tag:shop_cate|shopcate(###,'cate_name')]</span>
-
<span>商家名称:<a href="[tag:shop_id|U('Shangjia/index','shop_id=###')]">[tag:shop_name]</a></span>
-
<span>发布时间:[tag:create_time|date('Y-m-d H:i:s',###)]</span>
-
</li>
-
</shoplist>
复制代码
原理说明:通过tag引擎读取模板,然后读取页面里面的标签以及html内容,然后经过处理在显示出来1、创建自定义标签扩展文件
文件位置:网站目录\Lib\TagLib
没有TagLib文件夹的,自己建立一个,然后在TagLib目录建立标签库文件
自定义标签库的命名格式:"TagLib"+"Shop"+".class.php",其中Shop"是自定义的,第一个字母大写。
创建一个标签文件 TagLibShop.class.php
-
<?php
-
/*
-
* 商家模板标签库
-
*
-
*/
-
-
class TagLibShop extends TagLib{
-
-
// protected $tags 定义调用标签
-
protected $tags = array(
-
// '标签名'=>array('attr'=>'传入参数变量,传入参数变量','close'=>1) //close 是否闭合(0 或者1 默认为1,表示闭合)
-
'shoplist'=> array('attr'=> 'shopcate,cate,area,business,limit,order','close'=>1),
-
'shopinfo'=> array('attr'=> 'name,shopid,fun','close'=>0),
-
);
-
-
//列表类自定义标签
-
public function _shoplist($attr,$content){
-
$attr = $this->parseXmlAttr($attr); //必须有,读取模板传入参数
-
-
//逻辑处理
-
//接收各个参数分配到变量
-
$shop_cate = $attr['shopcate'];
-
$cate_id = $attr['cate'];
-
$area_id = $attr['area'];
-
$business_id = $attr['business'];
-
-
$order = $attr['order']; //排序
-
$limit = $attr['limit']; //调用数据区间
-
-
//经营范围查询
-
if($shop_cate){$w = ($w)?$w.' AND shop_cate ='.$shop_cate:'shop_cate ='.$shop_cate;}
-
//商家分类查询
-
if($cate_id){$w = ($w)?$w.' AND cate_id ='.$cate_id:'cate_id ='.$cate_id;}
-
//地区
-
if($area_id){$w = ($w)?$w.' AND area_id ='.$area_id:'area_id ='.$area_id;}
-
//商圈
-
if($business_id){$w = ($w)?$w.' AND business_id ='.$business_id:'business_id ='.$business_id;}
-
-
//得到数据
-
$list = M("shop")->where($w)->limit($limit)->order($order)->select();
-
$str = TagLib_list($content,$list); //模板数据处理
-
return $str; //输出
-
-
}
-
-
//根据传入值输出某个字段
-
public function _shopinfo($attr,$content){
-
$attr = $this->parseXmlAttr($attr);
-
-
$shop_id = $_GET['shop_id'];
-
$shop_id = ($attr['shopid'])?$attr['shopid']:$shop_id;
-
$key = $attr['name'];
-
$fun = $attr['fun'];
-
$info = M('shop')->table('bao_shop a,bao_shop_details b')->where('a.shop_id='.$shop_id.' AND b.shop_id='.$shop_id)->find();
-
-
$str =TagLib_info($info[$key],$fun); //模板数据处理
-
return $str;
-
}
-
}
复制代码
以上文件数据处理写入单独的函数库中,函数库文件如下,该函数添加到你自己的函数库库中
-
<?php
-
/*
-
* 系统标签处理函数
-
* TagLib扩展标签处理
-
* 作者:漠皓
-
*/
-
-
/*
-
* list标签格式化
-
* 参数:$content 模板内容 $list 将要输出的数据,格式为二维数组
-
* 返回值:$str 处理后的字符串数据
-
* 用法:taglist为标签名 参数='参数值'
-
* <taglist name='name'>
-
* <li>[tag:shop_id][tag:create_time|date('Y-m-d H:i:s',###)]</li>
-
* </taglist >
-
* 标签内可以使用函数,函数在字段后面用|隔开,其中###为传入值
-
*/
-
function TagLib_list($content,$list){
-
-
preg_match_all('/\[tag:(.*?)\]/',$content,$arry); //读取标签
-
$tag = $arry[0]; //匹配标签
-
$key = $arry[1]; //标签字段
-
$str='';
-
for($i=0;$i<count($list);$i++)
-
{
-
$c = $content; //读取模板内容
-
//替换标签
-
foreach($tag as $k=>$v){
-
//分割字符串,如果有函数那么执行函数后在输出
-
$arr = explode('|',$key[$k]);
-
$th = $list[$i][$arr[0]];
-
if($arr[1]){
-
$arr[1] = str_replace('###',$list[$i][$arr[0]],$arr[1]);
-
$a = '$th'." =$arr[1]";
-
eval($a.';');
-
}
-
$c = str_replace($v,$th,$c);
-
}
-
$str.=$c;
-
}
-
return $str;
-
}
-
-
/*
-
* info标签格式化
-
* 参数:$key 要处理的值 $fun 从模板中传入的要执行的函数
-
* 返回值:字符串
-
*/
-
function TagLib_info($key,$fun){
-
//有输出值则输出
-
if($key){
-
//如果使用函数
-
if($fun){$return = str_replace('###',$key,$fun);}else{$return =$key;}
-
//如果值不为空
-
if($return){
-
$str = '<?php ';
-
$str .= 'echo '.$return .';';
-
$str .= ' ?>';
-
}
-
}
-
return $str;
-
}
复制代码
2、配置项目配置文件 Conf\config.php配置文件加上下面三个配置项:
-
'TAGLIB_LOAD' => true,//加载标签库打开
-
'APP_AUTOLOAD_PATH' =>'@.TagLib',
-
'TAGLIB_BUILD_IN' =>'Cx,Shop', //Cx为核心标签库名称,Shop为自定义标签库名称,不能弄错。
复制代码
现在可以使用上面的标签进行调用数据啦
转自:http://www.thinkphp.cn/topic/31574.html
版权声明:若无特殊注明,本文皆为( yueshuo )原创,转载请保留文章出处。