使用过开源程序的朋友肯定都用过模板调用标签:例如 织梦dedecms
  1. {dede:arclist flag='h' limit='1,4'}
  2.     <div class='d1arc'><a href="[field:arcurl/]">[field:title/]</a></div>
  3. {/dede:arclist}
复制代码
然而在TP中也提供了一些标签,但是在大量展示页面时需要制作太多的控制器,而在控制器中要大量的调用重复数据,所以为了更好的简约代码提高效率,在TP中也可以实现自定义标签。

最终效果:该方法可以在标签中使用函数
  1. <shoplist name=''>
  2.     <li>
  3.         <span>商家ID:[tag:shop_id]</span>
  4.         <span>商家分类:[tag:shop_cate|shopcate(###,'cate_name')]</span>
  5.         <span>商家名称:<a href="[tag:shop_id|U('Shangjia/index','shop_id=###')]">[tag:shop_name]</a></span>
  6.         <span>发布时间:[tag:create_time|date('Y-m-d H:i:s',###)]</span>
  7.     </li>
  8. </shoplist>
复制代码
原理说明:通过tag引擎读取模板,然后读取页面里面的标签以及html内容,然后经过处理在显示出来

1、创建自定义标签扩展文件

文件位置:网站目录\Lib\TagLib

没有TagLib文件夹的,自己建立一个,然后在TagLib目录建立标签库文件

自定义标签库的命名格式:"TagLib"+"Shop"+".class.php",其中Shop"是自定义的,第一个字母大写。

创建一个标签文件 TagLibShop.class.php
  1. <?php
  2. /*
  3.  *    商家模板标签库
  4.  *
  5.  */
  6. class TagLibShop extends TagLib{
  7.     
  8.     // protected $tags 定义调用标签
  9.     protected $tags = array(
  10.         // '标签名'=>array('attr'=>'传入参数变量,传入参数变量','close'=>1) //close 是否闭合(0 或者1 默认为1,表示闭合)
  11.         'shoplist'=> array('attr'=> 'shopcate,cate,area,business,limit,order','close'=>1),
  12.         'shopinfo'=> array('attr'=> 'name,shopid,fun','close'=>0),
  13.     );
  14.     
  15.     //列表类自定义标签
  16.     public function _shoplist($attr,$content){
  17.         $attr = $this->parseXmlAttr($attr);  //必须有,读取模板传入参数
  18.         
  19.         //逻辑处理
  20.         //接收各个参数分配到变量
  21.         $shop_cate   = $attr['shopcate'];
  22.         $cate_id     = $attr['cate'];
  23.         $area_id     = $attr['area'];
  24.         $business_id = $attr['business'];
  25.         
  26.         $order       = $attr['order']; //排序
  27.         $limit         = $attr['limit']; //调用数据区间
  28.         
  29.         //经营范围查询
  30.         if($shop_cate){$w = ($w)?$w.' AND shop_cate ='.$shop_cate:'shop_cate ='.$shop_cate;}
  31.         //商家分类查询
  32.         if($cate_id){$w = ($w)?$w.' AND cate_id ='.$cate_id:'cate_id ='.$cate_id;}
  33.         //地区
  34.         if($area_id){$w = ($w)?$w.' AND area_id ='.$area_id:'area_id ='.$area_id;}
  35.         //商圈
  36.         if($business_id){$w = ($w)?$w.' AND business_id ='.$business_id:'business_id ='.$business_id;}
  37.         
  38.         //得到数据
  39.         $list = M("shop")->where($w)->limit($limit)->order($order)->select();
  40.         $str = TagLib_list($content,$list); //模板数据处理
  41.         return $str; //输出
  42.         
  43.     }
  44.     
  45.     //根据传入值输出某个字段
  46.     public function _shopinfo($attr,$content){
  47.         $attr = $this->parseXmlAttr($attr);
  48.         
  49.         $shop_id = $_GET['shop_id'];
  50.         $shop_id = ($attr['shopid'])?$attr['shopid']:$shop_id;
  51.         $key      = $attr['name'];
  52.         $fun      = $attr['fun'];
  53.         $info      = M('shop')->table('bao_shop a,bao_shop_details b')->where('a.shop_id='.$shop_id.' AND b.shop_id='.$shop_id)->find();
  54.         
  55.         $str =TagLib_info($info[$key],$fun); //模板数据处理
  56.         return $str;
  57.     }
  58. }
复制代码
以上文件数据处理写入单独的函数库中,函数库文件如下,该函数添加到你自己的函数库库中
  1. <?php
  2. /*
  3.  *    系统标签处理函数
  4.  *    TagLib扩展标签处理
  5.  *    作者:漠皓
  6.  */
  7.  
  8. /*    
  9.  *  list标签格式化
  10.  *    参数:$content 模板内容 $list  将要输出的数据,格式为二维数组
  11.  *    返回值:$str 处理后的字符串数据
  12.  *    用法:taglist为标签名 参数='参数值'
  13.  *    <taglist name='name'>
  14.  *        <li>[tag:shop_id][tag:create_time|date('Y-m-d H:i:s',###)]</li>
  15.  *    </taglist >
  16.  *    标签内可以使用函数,函数在字段后面用|隔开,其中###为传入值
  17.  */
  18. function TagLib_list($content,$list){
  19.     
  20.     preg_match_all('/\[tag:(.*?)\]/',$content,$arry); //读取标签
  21.     $tag = $arry[0]; //匹配标签
  22.     $key = $arry[1]; //标签字段
  23.     $str='';
  24.     for($i=0;$i<count($list);$i++)
  25.     {
  26.         $c = $content; //读取模板内容
  27.         //替换标签
  28.         foreach($tag as $k=>$v){
  29.             //分割字符串,如果有函数那么执行函数后在输出
  30.             $arr = explode('|',$key[$k]);
  31.             $th = $list[$i][$arr[0]];
  32.             if($arr[1]){
  33.                 $arr[1] = str_replace('###',$list[$i][$arr[0]],$arr[1]);
  34.                 $a = '$th'." =$arr[1]";
  35.                 eval($a.';');
  36.             }
  37.             $c = str_replace($v,$th,$c);
  38.         }
  39.         $str.=$c;
  40.     }
  41.     return $str;
  42. }
  43. /*
  44.  *    info标签格式化
  45.  *    参数:$key 要处理的值  $fun 从模板中传入的要执行的函数
  46.  *    返回值:字符串
  47.  */
  48. function TagLib_info($key,$fun){
  49.     //有输出值则输出
  50.     if($key){
  51.         //如果使用函数
  52.         if($fun){$return  = str_replace('###',$key,$fun);}else{$return =$key;}
  53.         //如果值不为空
  54.         if($return){
  55.             $str  = '<?php ';
  56.             $str .= 'echo '.$return .';';
  57.             $str .= ?>';
  58.         }
  59.     }
  60.     return $str;
  61. }
复制代码
2、配置项目配置文件 Conf\config.php配置文件加上下面三个配置项:
  1. 'TAGLIB_LOAD'               => true,//加载标签库打开
  2. 'APP_AUTOLOAD_PATH'         =>'@.TagLib',
  3. 'TAGLIB_BUILD_IN'           =>'Cx,Shop', //Cx为核心标签库名称,Shop为自定义标签库名称,不能弄错。
复制代码

现在可以使用上面的标签进行调用数据啦


转自:http://www.thinkphp.cn/topic/31574.html

版权声明:若无特殊注明,本文皆为( yueshuo )原创,转载请保留文章出处。