请高人指正
TagLib/TagLibFront.class.php
/**
* @author Meik
* @homepage http://www.xuzc.cn
* @filename TagLibFront.class.php
* @encoding UTF-8
* @datetime 2011-6-27 14:59:38
* @version 1.0
* @Description
*/
class TagLibFront extends TagLib {
protected $tags = array(
'list' => array('attr' => 'table,where,order,limit,relation,field,result,page,purl,purlvars', 'close' => 1),
);
//获取列表数据 可关联(字段过滤无效) 可分页
public function _list($attr, $content) {
$tag = $this->parseXmlAttr($attr, 'list');
$result = !empty($tag['result']) ? $tag['result'] : 'data';
if (!empty($tag['table'])) {
$model = 'D("' . $tag['table'] . '")';
} else {
return '';
}
$key = !empty($tag['key']) ? $tag['key'] : 'i';
$mod = isset($tag['mod']) ? $tag['mod'] : '2';
//拼接SQL查询语句
$parseStr = '<?php ';
if($tag['where']!= null && $tag['where']=='$where') {
$parseStr .=$tag['where'] != null ? '$map='.$tag["where"].';' : '';
}else {
$parseStr .=$tag['where'] != null ? '$map=array(' . trim(str_replace("=", "=>", $tag["where"]),',') . ');' : '';
}
if ($tag["page"]) {
$parseStr .= '$' . $result . '_count=' . $model;
$parseStr .= $tag['where'] != null ? '->where($map)' : '';
$parseStr .='->cache(true)->count(' . $model . '->getPk());';
$parseStr .= 'import("ORG.Util.Page"); $' . $result . '_p = new Page($' . $result . '_count, ' . $tag["page"] . ');';
if($tag['purlvars']!=null) {
$parseStr .= '$' . $result . '_page = $' . $result . '_p->show("'.$tag['purl'].'",'.$tag['purlvars'].');';
}else {
$parseStr .= '$' . $result . '_page = $' . $result . '_p->show("'.$tag['purl'].'");';
}
}
$parseStr .= '$' . $result . '_result = ' . $model ;
$parseStr .= $tag['relation'] != null ? '->relation(array(' . trim($tag["relation"],',') . '))' : '';
$parseStr .= $tag['where'] != null ? '->where($map)' : '';
$parseStr .= $tag['field'] != null ? '->field("'.trim($tag["field"],',').'")' : '';
$parseStr .= $tag['order'] != null ? '->order("'.trim($tag["order"],',').'")' : '';
if ($tag["page"]) {
$parseStr .= $tag['page'] != null ? '->limit("$' . $result . '_p->firstRow , $' . $result . '_p->listRows")' : '';
}else {
$parseStr .= $tag['limit'] != null ? '->limit("'.trim($tag["limit"],',').'")' : '';
}
$parseStr .= '->select();?>';
$parseStr .= '<?php if($' . $result . '_result){ $' . $key . '=0;';
$parseStr .= 'foreach($' . $result . '_result as $key=>$' . $result . '){ ?>';
$parseStr .= '<?php ++$' . $key . ';$mod = ($' . $key . ' % ' . $mod . ');?>';
$parseStr .= $content;
$parseStr .= '<?php }};?>';
return $parseStr;
}
}
用法 :任意模板文件
<Front:list table="XinwenNeirong" where="'flid'=array('eq',5)" field="bt,id" order="lrsj DESC" result="data" limit="4">
<li><a href="{:U('News/View/index',array('id'=>$data[id]))}">·{$data.bt}</a></li>
</Front:list>