'taglib_build_in' => 'cx,tags', // 内置标签库名称(标签使用不必指定标签库名称),以逗号分隔 注意解析顺序
namespace think\template\taglib;
use think\template\TagLib;
class Tags extends TagLib{
/**
* 定义标签列表
*/
protected $tags = [
'sel' => ['attr' => 'table,id,where,order,limit,field,page'],
'find' => ['attr' => 'table,id,where,field'],
'val' => ['attr' => 'table,where,value,getdata','close' => 0],
];
/**
* volist标签解析 循环输出数据集
* 格式:
* {volist name="userList" id="user" empty=""}
* {user.username}
* {user.email}
* {/volist}
* @access public
* @param array $tag 标签属性
* @param string $content 标签内容
* @return string|void
*/
public function tagsel($tag, $content)
{
$table = $tag['table'];
$id = isset($tag['id'])?$tag['id']:'vo';
$where = isset($tag['where'])?$tag['where']:'[]';
$order = isset($tag['order'])?$tag['order']:'id desc';
$limit = !empty($tag['limit']) ? $tag['limit'] : '0';
$field = isset($tag['field'])?$tag['field']:'*';
$page = isset($tag['page'])?$tag['page']:false;
if($page){
$pa=input('');
if($pa){
foreach($pa as $key => $val){
$string[$key] = stripslashes($val);
}
$pa = $string;
}else{
$pa=[];
}
$querys= var_export(['query'=>$pa,'var_page'=>$page],TRUE);
}else{
$querys='[]';
}
$parseStr = ' //查看是否存在model类
$file=[APP_PATH.'common/model/'.ucfirst($table).'.php',APP_PATH.request()->module().'model/'.ucfirst($table).'.php'];
if(file_exists($file[0])) $parseStr .= '$db=model("app\common\model\\'.ucfirst($table).'");';
if(file_exists($file[1])) $parseStr .= '$db=model(app\\'.request()->module().'\model\\'.ucfirst($table).');';
if(!file_exists($file[0]) && !file_exists($file[1])){
$parseStr .= '$db=db("'.$table.'");';
}
$parseStr .= $page?"\$seldata=\$db->where($where)->field('$field')->order('$order')->paginate('$limit',false,$querys);":"\$seldata=\$db->where($where)->field('$field')->order('$order')->limit('$limit')->select();";
// 设置了输出数组长度
$parseStr .= 'foreach($seldata as $key=>$' . $id . '): ?>';
$parseStr .= $content;
$parseStr .= ' ';
if ($page) {
$parseStr .= 'render(); ?>';
}
if (!empty($parseStr)) {
return $parseStr;
}
return;
}
public function tagfind($tag, $content){
$table = $tag['table'];
$id = isset($tag['id'])?$tag['id']:'vo';
$where = isset($tag['where'])?$tag['where']:'[]';
$field = isset($tag['field'])?$tag['field']:'*';
$parseStr = '
//查看是否存在model类
$file=[APP_PATH.'common/model/'.ucfirst($table).'.php',APP_PATH.request()->module().'model/'.ucfirst($table).'.php'];
if(file_exists($file[0])) $parseStr .= '$db=model("app\common\model\\'.ucfirst($table).'");';
if(file_exists($file[1])) $parseStr .= '$db=model(app\\'.request()->module().'\model\\'.ucfirst($table).');';
if(!file_exists($file[0]) && !file_exists($file[1])){
$parseStr .= '$db=db("'.$table.'");';
}
$parseStr .= '$'.$id."=\$db->where($where)->field('$field')->find(); ?>";
$parseStr .= $content;
if (!empty($parseStr)) {
return $parseStr;
}
return;
}
public function tagval($tag){
$table = $tag['table'];
$where = isset($tag['where'])?$tag['where']:'[]';
$value = isset($tag['value'])?$tag['value']:'id';
$getdata = isset($tag['getdata'])?$tag['getdata']:false;
$parseStr = '
//查看是否存在model类
$file=[APP_PATH.'common/model/'.ucfirst($table).'.php',APP_PATH.request()->module().'model/'.ucfirst($table).'.php'];
if(file_exists($file[0])) $parseStr .= '$db=model("app\common\model\\'.ucfirst($table).'");';
if(file_exists($file[1])) $parseStr .= '$db=model(app\\'.request()->module().'\model\\'.ucfirst($table).');';
if(file_exists($file[1])) $parseStr .= 'use app\\'.request()->module().'\model\\'.ucfirst($table).'; $db=model("'.ucfirst($table).'");';
if(!file_exists($file[0]) && !file_exists($file[1])){
$parseStr .= '$db=db("'.$table.'");';
}
$parseStr .= "\$arr=\$db->where($where)->field('$value')->find(); ";
$parseStr .= $getdata ?'echo $arr->getdata("'.$value.'");?>' :'echo $arr["'.$value.'"];?>';
if (!empty($parseStr)) {
return $parseStr;
}
return;
}
}