tinkphp5自定义模板标签

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}&nbsp;</span>
      <span class="views"> 阅读 {$vo.views}&nbsp;</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}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值