dd

foolist万能标签

jerry thinkphp 2015年11月18日 收藏
dede的万能标签好厉害,这里也弄个
之前在论坛中也发过,现在在这里也发个,比上次的版本添加了,缓存,调试,field等..
foo是什么意义通俗点就是无法识别,一塌糊涂的意思,就是没意义。
这个没意义好有万能的意味,姑且使用foolist吧。
至于万能标签的意义呢,懒人必备,cms必备啊
官方的volist是要在action中定义数据来源的,foolist就厉害了,无需定义鸟:
foolist也是一个CX标签,
修改Lib\Driver\TagLib\TagLibCx.class.php
添加标签定义:
//一下为自定义
        'foolist'   =>  array('attr'=>'model,where,order,num,id,page,pagesize,query,flag,field,cache','level'=>3),  //万能的输出标签
好了下面是foolist 的内容
<?php
//by 无语西风
// Lib/Driver/TagLid/TagLiCx.class.php   
//万能标签 <foolist model='catalog' where='' order='' num='10' page='true' pagesize='15' field='' >
//'foolist'=>array('attr'=>'model,where,order,num,id,page,pagesize,query,flag,debug','level'=>3),  //万能的输出标签
    public function _foolist($attr,$content)
    {
         $html='';         
         $tag       = $this->parseXmlAttr($attr,'foolist');         
         $model     =!empty($tag['model'])?$tag['model']:'';     
         $order     =!empty($tag['order'])?$tag['order']:'';
         $num       =!empty($tag['num'])?$tag['num']:'';
         $id        =!empty($tag['id'])?$tag['id']:'d';
         $where     =!empty($tag['where'])?$tag['where']:'';
         //使where支持 条件判断,添加不等于的判断         
         $this->comparison['noteq']= '<>';
         $this->comparison['sqleq']= '=';
         $where     =$this->parseCondition($where); 
         $page=false;
         if(!empty($tag['page'])) $page=$tag['page'];
         if($page=='ture') $page=false;         
         $pagesize  =!empty($tag['pagesize'])?$tag['pagesize']:'10';
         //是否用缓存,默认是false
         $cache     =!empty($tag['cache'])?$tag['cache']:false;
         $query     =!empty($tag['query'])?$tag['query']:'';
         $field     =!empty($tag['field'])?$tag['field']:'';
         $debug     =!empty($tag['debug'])?$tag['debug']:false;
         //使query 支持条件判断
         $query     =$this->parseCondition($query); 
         if($where!='')  $where.=' and '.$flag; 
         $html.='<?php $m=D("'.$model.'");';
         //如果使用了query,将忽略使用where,num,order,page,field,cache 等,使用query无法实现分页
         if($query){  
             if($cache!=false){
                $html.='$cache_key="key_".md5("'.$query.'");';
                $html.='if(!$ret=S($cache_key)){ $ret=$m->query("'.$query.'");S($cache_key,$ret);}';
             }else{
                $html.='$ret=$m->query("'.$query.'");';
             }                         
         }
         //如果使用了分页,缓存也不生效
         if($page && !$query){
               $html.='import("@.Common.Page"); ';    //这里改成你的Page类           
               $html.='$count=$m->where("'.$where.'")->count();';
               $html.='$p = new Page ( $count, '.$pagesize.' );';
               //如果使用了分页,num将不起作用
               $html.='$ret=$m->field("'.$field.'")->where("'.$where.'")->limit($p->firstRow.",".$p->listRows)->order("'.$order.'")->select();';
               $html.='$cutInfo ="<div class=cutinfo > ". $p->show ()."</div>";'; 
         }
         //如果没有使用分页,并且没有 query
         if(!$page && !$query){    
              //有缓存
              if($cache!=false){
                  //包含缓存判断
                  $html.='$cache_key="key_".md5($m->field("'.$field.'")->where("'.$where.'")->order("'.$order.'")->limit("'.$num.'")->select(false));';
                  $html.='if(!$ret=S($cache_key)){ $ret=$m->field("'.$field.'")->where("'.$where.'")->order("'.$order.'")->limit("'.$num.'")->select(); S($cache_key,$ret,'.$cache.'); }';
              }else{
                  //没有缓存
                  $html.='$ret=$m->field("'.$field.'")->where("'.$where.'")->order("'.$order.'")->limit("'.$num.'")->select();';
              }
              
         }        
         if($debug!=false){
                 $html.='dump($ret);dump($m->getLastSql());';
         }
         $html.='foreach($ret as $key=>$'.$id.'): ?>';
         $html.=$this->tpl->parse($content);            
         $html.='<?php endforeach; ?>';        
         if($page)    $html.='<?php echo $cutInfo;?>'; 
         return $html;
         
    }
这个标签任何版本都通用,(果然很万能),好了现在说说怎么使用
获得文章状态为1的文章列表:
<foolist model="article" where="status sqleq  1">
    <li><a href={$d.url} >{$d.title}</a></li>
</foolist>
哎就这么简单,默认id就是为d,如果你要定义就要这样使用,
<foolist model="article" where="status sqleq  1" id="vo">
    <li><a href={$vo.url} >{$vo.title}</a></li>
</foolist>
还有如果你要使用:
<foolist model="article" where="status=1" id="vo">
    <li><a href={$vo.url} >{$vo.title}</a></li>
</foolist>
就会报错,没错在任何CX标签中你不能使用=,大于号,小于号,
这个呢官方有介绍,也可查看CX父类的定义:
\Lib\Template\TagLib.class.php
    protected $comparison = array(' nheq '=>' !== ',' heq '=>' === ',' neq '=>' != ',' eq '=>' == ',' egt '=>' >= ',' gt '=>' > ',' elt '=>' <= ',' lt '=>' < ');
而,mysql使用的=和<> ,请使用 sqleq 和 noteq 代替

好了换个吧,如果想指定字段,排序就这样:
<foolist model="article" field="url,title" where="status sqleq  1" order="uptime desc">
    <li><a href={$d.url} >{$d.title}</a></li>
</foolist>
现在是否明白了,只要你回使用model,就会使用介个了。
限制查询数量请使用num,比如只输出10条数据:
<foolist model="article" field="url,title" where="status sqleq 1" order="uptime desc" num="10">
    <li><a href={$d.url} >{$d.title}</a></li>
</foolist>
好了下面换点口味。
万能标签是能分页的,你可以这样使用:
<foolist model="article" field="url,title" where="status sqleq  1" order="uptime desc" page="true" pagesize="10">
    <li><a href={$d.url} >{$d.title}</a></li>
</foolist>
page默认是false,打开之后定义pagesize就好了,分页的内容使用的就是官方的page类,样式自己再调整下就ok了。
foolist当然支持query实现更复杂的查询:
<foolist query="select * from article">
    <li><a href={$d.url} >{$d.title}</a></li>
</foolist>
不过此时的where,num,order,page,field,cache参数将无法使用。
没有在action定义数据来源,foolist也是能实现缓存的,比如对目录查询缓存1小时:
<foolist model="catalog" where="status sqleq  1" cache="60*60">
    <li><a href={$d.url} >{$d.title}</a></li>
</foolist>
cache默认是关闭的,想开启缓存直接定义cache为你想缓存的时间即可。
debug的玩法:
<foolist model="catalog" where="status sqleq 1" cache="60*60" debug="true">
    <li><a href={$d.url} >{$d.title}</a></li>
</foolist>
如果你想知道foolist到底有什么数据,请开启debug参数,
此时会dump数据集,和getLastSql() 。
下面说说如何传递$_GET,如果你想或的当前目录下的所有文章,假如$_GET['id']就是目录的id,如此就可以这样使用:
<foolist model="article" where="$_GET[id] sqleq pid " >
    <li><a href={$d.url} >{$d.title}</a></li>
</foolist>
//====================
foolist使用的model都是通过D实现的,所以想实现更复杂的数据模型,请自己定义自己的model类即可。
如果在数据输出之前想实现更多的业务逻辑,就使用官方的volist,这也是区别所在。
假如你开发了cms,前台的所有风格模板只要使用一个标签就能搞定,方便不,
方便面。。。。。
dd