阅读 82

Taglib自定义万能标签扩展 DownLoad

Taglib自定义万能标签扩展 DownLoad

http://www.thinkphp.cn/extend/538.html

用ThinkPHP的标签做网站觉得不够快速,就自己写了一个扩展,感觉挺好的,分享出来,给有需要的TPER。

复制代码

    <?php
      class TagLibEP extends TagLib{
        protected $tags   =  array('select'=>array('attr'=>'table,where,order,limit,id,page,sql,field,key,mod,debug','level'=>3));
        public function _select($attr,$content){
             $tag       = $this->parseXmlAttr($attr,'select');
             $table     =!empty($tag['table'])?$tag['table']:'';
             $order     =!empty($tag['order'])?$tag['order']:'';
             $limit     =!empty($tag['limit'])?intval($tag['limit']):'';
             $id        =!empty($tag['id'])?$tag['id']:'r';
             $where     =!empty($tag['where'])?$tag['where']:' 1 ';
             $key        =!empty($tag['key'])?$tag['key']:'i';
             $mod        =!empty($tag['mod'])?$tag['mod']:'2';
             $page      =!empty($tag['page'])?$tag['page']:false;
             $sql         =!empty($tag['sql'])?$tag['sql']:'';
             $field     =!empty($tag['field'])?$tag['field']:'';
             $debug     =!empty($tag['debug'])?$tag['debug']:false;
             $this->comparison['noteq']= '<>';
             $this->comparison['sqleq']= '=';
             $where     =$this->parseCondition($where);
             $sql         =$this->parseCondition($sql);
             $parsestr.='<?php $m=M("'.$table.'");';
             
             if($sql){
                if($page){
                    $limit=$limit?$limit:10;//如果有page,没有输入limit则默认为10
                    $parsestr.='import("@.ORG.Page");';
                    $parsestr.='$count=count($m->query("'.$sql.'"));';
                    $parsestr.='$p = new Page ( $count, '.$limit.' );';
                    $parsestr.='$sql.="'.$sql.'";';

                    $parsestr.='$sql.=" limit ".$p->firstRow.",".$p->listRows."";';
                    $parsestr.='$ret=$m->query($sql);';
                    $parsestr.='$pages=$p->show();';
                    //$parsestr.='dump($count);dump($sql);';
                }else{
                    $sql.=$limit?(' limit '.$limit):'';
                    $parsestr.='$ret=$m->query("'.$sql.'");'; 
                }
             }else{
                 if($page){
                     $limit=$limit?$limit:10;//如果有page,没有输入limit则默认为10
                     $parsestr.='import("@.ORG.Page");'; 
                     $parsestr.='$count=$m->where("'.$where.'")->count();';
                     $parsestr.='$p = new Page ( $count, '.$limit.' );';
                     $parsestr.='$ret=$m->field("'.$field.'")->where("'.$where.'")->limit($p->firstRow.",".$p->listRows)->order("'.$order.'")->select();';
                     $parsestr.='$pages=$p->show();';
                 }else{
                     $parsestr.='$ret=$m->field("'.$field.'")->where("'.$where.'")->order("'.$order.'")->limit("'.$limit.'")->select();'; 
                 }            
             }      
             if($debug!=false){
                $parsestr.='dump($ret);dump($m->getLastSql());';
             }
             $parsestr.= 'if ($ret): $'.$key.'=0;';
             $parsestr.= 'foreach($ret as $key=>$'.$id.'):';
             $parsestr.= '++$'.$key.';$mod = ($'.$key.' % '.$mod.' );?>';
             $parsestr.= $this->tpl->parse($content);      
             $parsestr.= '<?php endforeach;endif;?>';       
             return $parsestr;
             
        }
    }
    ?>

DEMO:
文件放在ThinkPHP\Extend\Driver\TagLib\下,然后在config.php文件中引入,'TAGLIB_PRE_LOAD' => 'EP',
table:表名,where查询条件,order排序,id结果变量,page分页开启,field字段,key循环变量,mod取模,debug是否调试

  1. <EP:select sql="select * from table order by id desc" limit="10" page="1"></EP:select>

  2. <EP:select table="article" limit="10"></EP:select>

复制代码

无论是频道页面还是列表页面都可以实现分页了。


    1. <div id="pages">{$pages}</div>


文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐