自定义标签为Migo.class.php 放置Application/Taglib目录下!!代码如下:
/** Migo 自定义标签调用
* @author Caption
*/
namespace TagLib;
use Think\Template\TagLib;
class Migo extends TagLib {
protected $tags = array(
'list' => array('attr' => 'type,order,limit,where,join,field','close' => 1),
);
public function _list($attr,$content) {
$type = $attr['type']; //要查询的数据表
$order = $attr['order']; //排序
$limit = $attr['limit']; //多少条数据
$where = $attr['where']; //查询条件
$join = $attr['join'];
$field= $attr['field'];
if($field == '')
$field ='*';
$str = '<?php ';
if($join =='')
$str .= '$result = M("' . $type . '")->alias("a")->field("'.$field.'")->where("' . $where . '")->order("' . $order . '")->limit(' . $limit . ')->select();';
else
$str .= '$result = M("' . $type . '")->alias("a")->field("'.$field.'")->join("'.$join.'")->where("' . $where . '")->order("' . $order . '")->limit(' . $limit . ')->select();';
$str .= 'foreach ($result as $vo):';
$str .= '?>';
$str .= $content;
$str .= '<?php endforeach ?>';
return $str;
}
}
注意命名空间的定义:
namespace TagLib;
否则你无法使用哦,会报ThinkPHP自定义标签库无法加载,模板报“实例化一个不存在的类!:TagLibXXXX”的错误
下面页面调用就可以了,代码如下:
{$vo['id']}{$vo['title']|msubstr=0,22,'utf-8',true}{$vo['name']}{$vo['createtime']} $vo['id']))}" class="modify gray">编辑 $vo['id']))}'}">删除