tinkphp5自定义模板标签
最新文章
(一)标签定义
增加两个属性table(数据表)、field(表字段),方便多模型调用
protected $tags = [
'menu' => ['attr' => 'name', 'close' => 1], //0闭合标签 ,1不闭合(默认)
'hot' => ['attr' => 'catid,num,name,table,field', 'close' => 1],
'recommend' => ['attr' => 'catid,num,name,table,field', 'close' => 1],
'list' => ['attr' => 'catid,num,name,table,field', 'close' => 1],
'page' => ['attr' => 'catid', 'close' => 1], //实际不使用,仅供教学参考
];
(二)标签申明
public function tagList($tag, $content) {
$table = $tag['table'];
$field = $tag['field'];
//首页cid=0,num>0 ; 分类页cid>0,num=0
if(empty($tag['catid']) || $tag['catid']=='0'){
$num = $tag['num'];
$php = <<<php
<?php
\$list = think\Db::name('$table')->field('$field')->limit("$num")->order('listorder desc')->cache('$table_index',config('cache.expire'))->select();
\$__LIST__ = \$list;
?>
php;
}else{
$num = 0;
$where = "catid=" . $tag['catid'];
$php = <<<php
<?php
\$list = think\Db::name('$table')->where("$where")->field('$field')->limit("$num")->order('listorder desc')->paginate();
\$__LIST__ = \$list;
?>
php;
}
$name = $tag['name'];
$parse = $php;
$parse .= '{volist name="__LIST__" id="' . $name . '"}';
$parse .= $content;
$parse .= '{/volist}';
return $parse;
}
(三)标签调用
{def:list name="vo" catid="0" num="2" table="article" field="id,catid,title,thumb,description,inputtime,views"}
<article class="post type-post status-publish format-standard hentry">
<figure class="thumbnail">
<a href="{:url('info',['id'=>$vo.id])}"> <img src="__UPLOADS__/{$vo.thumb}" alt="{$vo.title}"></a>
<span class="cat"> <a href="{:url('category',['id'=>$vo.catid])}">{$vo.catid|getCatInfoById=catname}</a></span>
</figure>
<header class="entry-header">
<h2 class="entry-title"><a href="{:url('info',['id'=>$vo.id])}" rel="bookmark">{$vo.title}</a></h2>
</header>
<div class="entry-content">
<div class="archive-content">{$vo.description}</div>
<br/>
<span class="title-l"></span>
<span class="entry-meta">
<span class="date">{$vo.inputtime} </span>
<span class="views"> 阅读 {$vo.views} </span>
<span class="comment"><a href="{:url('info',['id'=>$vo.id])}"> <i class="fa fa-comment-o"></i> 查看评论</a></span>
</span>
<div class="clear"></div>
</div>
<span class="entry-more"> <a href="{:url('info',['id'=>$vo.id])}" rel="bookmark">阅读全文</a></span>
</article>
{/def:list}
|