| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 | <?phpdeclare(strict_types=1);namespace app\taglib;use think\template\TagLib;/** * 标签扩展 */class Tp extends TagLib{    /**     * 标签定义     *  attr 属性列表 close 是否闭合(0 或者1 默认1) alias 标签别名 level 嵌套层次     * @var array     */    protected $tags = array(        'close'    => ['attr' => 'time,format', 'close' => 0],                          // 闭合标签,默认为不闭合        'open'     => ['attr' => 'name,type', 'close' => 1],        'nav'      => ['attr' => 'id,limit,name', 'close' => 1],                        // 通用导航信息        // 'cate'     => ['attr' => 'id,type,anchor', 'close' => 0],                    // 通用栏目信息        // 'position' => ['attr' => 'name', 'close' => 1],                              // 通用位置信息        // 'link'     => ['attr' => 'name', 'close' => 1],                              // 获取友情链接        // 'ad'       => ['attr' => 'name,id', 'close' => 1],                           // 获取广告信息        'listbycid'     => ['attr' => 'cid,name,limit', 'close' => 1],                  // 通用列表        'listtime'  => ['attr' => 'name,limit', 'close' => 1],                          // 时间归档    );    // 这是一个闭合标签的简单演示    public function tagClose($tag)    {        $format = empty($tag['format']) ? 'Y-m-d H:i:s' : $tag['format'];        $time   = empty($tag['time']) ? time() : $tag['time'];        $parse  = '<?php ';        $parse  .= 'echo date("' . $format . '",' . $time . ');';        $parse  .= ' ?>';        return $parse;    }    // 这是一个非闭合标签的简单演示    public function tagOpen($tag, $content)    {        $type  = empty($tag['type']) ? 0 : 1; // 这个type目的是为了区分类型,一般来源是数据库        $name  = $tag['name'];                // name是必填项,这里不做判断了        $parse = '<?php ';        $parse .= '$test_arr=[[1,3,5,7,9],[2,4,6,8,10]];'; // 这里是模拟数据        $parse .= '$__LIST__ = $test_arr[' . $type . '];';        $parse .= ' ?>';        $parse .= '{volist name="__LIST__" id="' . $name . '"}';        $parse .= $content;        $parse .= '{/volist}';        return $parse;    }    // 文章列表    public function tagListbycid($tag, $content)    {        $cid    = (int) $tag['cid'];                        // 不可以为空        $name   = (string) $tag['name'];                     // 不可为空        $order  =  isset($tag['order']) ? (string) $tag['order'] : 'sort ASC,id DESC';  // 可为空        $limit  = $tag['limit'] ? (int) $tag['limit'] :  10; // 多少条数据                $parse = '<?php ';        $parse .= '$list = \app\model\Article::getListByCid(' . $cid . ', ' . $limit . ', "' . $order .'");';        $parse .= '?>';        $parse .= '{volist name="list" id="' . $name . '"}';        $parse .= $content;        $parse .= '{/volist}';                return $parse;    }    //     public function tagListtime($tag, $content)    {        $name   = (string) $tag['name'];                        // 不可为空        $limit  = $tag['limit'] ? (int) $tag['limit'] : 0;     // 多少条数据 0 不限制        $parse  = '<?php ';        $parse .= '$list = \app\model\Article::createTimeArchive(' . $limit . ');';        $parse .= '?>';        $parse .= '{volist name="list" id="' . $name . '"}';        $parse .= $content;        $parse .= '{/volist}';        return $parse;    }}
 |