• 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:
    文件放在ThinkPHPExtendDriverTagLib下,然后在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>
  • 相关阅读:
    Web前端学习第三天·fighting_常用的一些标签(一)
    Web前端学习第七天·fighting_CSS样式的编写和使用(二)
    Web前端学习第八天·fighting_使用CSS美化文字(一)
    WPF 自定义RadioButton样式
    WPF自定义控件与样式自定义按钮(Button)
    WPF 自定义Calendar样式(日历样式,周六周日红色显示)
    WPF 自定义TextBox带水印控件,可设置圆角
    WPF 自定义CheckBox样式
    常见web UI 元素操作 及API使用
    selenium定位下拉框
  • 原文地址:https://www.cnblogs.com/lxwphp/p/9212285.html
Copyright © 2020-2023  润新知