ThinkPHP5 自定义模版标签tablib
新建文件application/common/taglib/Diy.php
Diy.php
<?php
// +----------------------------------------------------------------------
// | 自定义模版标签
// +----------------------------------------------------------------------
// | Author: SenSen <1050575278> 2018-12
// +----------------------------------------------------------------------
namespace app\common\taglib;
use think\Db;
use think\template\TagLib;
class Sen extends TagLib
{
protected $tags = [
'arclist' => ['attr'=> 'field,id,limit,cid,order,name,flag,type', 'close'=>1],
'show' => ['attr'=> 'title,field', 'close'=>0],
];
/**
* 文章标签
* @param $tag
* @param $content
* @return string
*/
public function tagArclist($tag, $content)
{
$id = isset($tag['id']) ? $tag['id'] : 'vo';
$order = empty($tag['order']) ? "'id DESC'" : '"'.$tag['order'].'"';
$name = 'article';
$field = "''";
if (!empty($tag['field'])) {
if (strpos($tag['field'], '$') === 0) {
$field = $tag['field'];
$this->autoBuildVar($field);
} else {
$field = "'{$tag['field']}'";
}
}
$cid = "''";
if (!empty($tag['cid'])) {
if (strpos($tag['cid'], '$') === 0) {
$cid = $tag['cid'];
$this->autoBuildVar($cid);
} else {
$cid = "'{$tag['cid']}'";
}
}
$flag = "''";
if (!empty($tag['flag'])) {
if (strpos($tag['flag'], '$') === 0) {
$flag = $tag['flag'];
$this->autoBuildVar($flag);
} else {
$flag = "'{$tag['flag']}'";
}
}
$type = "''";
if (!empty($tag['type'])) {
if (strpos($tag['type'], '$') === 0) {
$type = $tag['type'];
$this->autoBuildVar($type);
} else {
$type = "'{$tag['type']}'";
}
}
$limit = "''";
if (!empty($tag['limit'])) {
if (strpos($tag['limit'], '$') === 0) {
$limit = $tag['limit'];
$this->autoBuildVar($limit);
} else {
$limit = "'{$tag['limit']}'";
}
}else{
$limit = '0,6';
}
$parse = <<<parse
<?php
\$$name = \app\common\model\Article::tagArticle([
'field' => {$field},
'cid'=>{$cid},
'limit'=>{$limit},
'flag'=>{$flag},
'type'=>{$type},
'order'=>{$order}
]);
?>
{volist name="{$name}" id="{$id}"}
{$content}
{/volist}
parse;
return $parse;
}
/**
* 广告标签
* @param $tag
* @return mixed
*/
public function tagShow($tag)
{
$title = $tag['title'] ? $tag['title'] : '';
$result = Db::name('ad')->where(['tag'=>$title, 'status'=>1])->find();
return $result['content'];
}
}
- 阅读剩余部分 -