1,首先在app下面新建common文件夹做为公共文件夹。
2,在common下面新建taglib来放我们的标签
3,新建一个类如ht,注意需要继承think\template\TagLib;
<?php namespace app\common\taglib; use think\template\TagLib; class ht extends TagLib { }
代码解构:
假如我们要定义一个获取文章列表的标签,代码如下
<?php namespace app\common\taglib; use think\template\TagLib; class ht extends TagLib { protected $tags =[ 'article' => ['attr'=>'name,length,id,typeid,titlelen,orderby,type,is_tui'], ]; // 文章调用 public function tagArticle($tag, $content) { $name = !empty($tag['name']) ? $tag['name'] : ''; $orderby = !empty($tag['orderby']) ? $tag['orderby'] : ''; $typeid = !empty($tag['typeid']) ? $tag['typeid']: 0; $is_tui = !empty($tag['is_tui']) ? $tag['is_tui']: 'all'; $typeid = $this->varOrvalue($typeid); $length = !empty($tag['length']) && is_numeric($tag['length']) ? intval($tag['length']) : 5; $titlelen = !empty($tag['titlelen']) && is_numeric($tag['titlelen']) ? intval($tag['titlelen']) : 100; $id = !empty($tag['id']) ? $tag['id'] : 'field'; $type = !empty($tag['type']) ? $tag['type'] : 'self'; $parse = '<?php '; $parse .= '$tagArtlist = new app\common\taglib\ht\tagArtList;'; // 在这里调用了另一个类,我们再定义一个类 $parse .= '$typeid = '.$typeid.';'; $parse .= '$Article = $tagArtlist->getArticle($typeid,'.$length.','.$titlelen.',"'.$orderby.'","'.$type.'","'.$is_tui.'");'; $parse .= '$__LIST__ = $Article;'; $parse .= ' ?>'; $parse .= '{volist name="__LIST__" length="' . $length .'" id="' . $id . '"'; $parse .= '"}'; $parse .= $content; $parse .= '{/volist}'; return $parse; } }
文件app\common\taglib\ht\tagArtList
<?php namespace app\common\taglib\ht; use app\common\model\Archives as ArcModel; use app\common\model\Arctype; class tagArtList extends Base { protected function _initialize() { parent::_initialize(); // TODO: Change the autogenerated stub } // 获取Article列表 public function getArticle($typeid,$length, $titlelen, $order,$type,$is_tui="all") { $page = $this->page; $typeid = $typeid ? $typeid : $this->tid; $where = [ ['status', '=', 1] ]; if ($is_tui != "all") { $where[] = ["is_tui", "=", $is_tui]; } $typeid = explode(",",$typeid); if ($type == "son") { $typeid2 = Arctype::where("status",1)->where("pid","in",$typeid)->column("id"); $typeid = array_merge($typeid,$typeid2); } if (!in_array(0,$typeid) && !in_array("all",$typeid)) { $where[] = [ ['arctype_id', 'in', $typeid] ]; } if (empty($order)) { $Article = ArcModel::where($where)->order('id desc') ->paginate(["list_rows"=>$length,"page"=>$page]); } elseif ($order == 'rand') { $Article = ArcModel::where($where)->orderRand() ->paginate(["list_rows"=>$length,"page"=>$page]);; } else { $Article = ArcModel::where($where)->order($order) ->paginate(["list_rows"=>$length,"page"=>$page]);; } foreach ($Article as $i=>$value) { if ($value->is_jump == 1) { $Article[$i]['href'] = $value->url; } else { $Article[$i]['href'] = url('Views/index', ['aid'=>$value->id]); } $Article[$i]['ltitle'] = mb_substr($Article[$i]['title'], 0, $titlelen); } return $Article; } }
这样一个获取文章列表的taglib标签就定义好,不过要想使用还需要在模板的配置下预加载标签
由于是多应用模式,哪个应用需要就在哪个应用下面新建config目录,新建view配置文件,把全局的view复制过来,加入一行,见上图。