1,首先在app下面新建common文件夾做為公共文件夾。
2,在common下面新建taglib來放我們的標(biāo)簽
3,新建一個類如ht,注意需要繼承think\template\TagLib;
<?php
namespace app\common\taglib;
use think\template\TagLib;
class ht extends TagLib
{
}代碼解構(gòu):

假如我們要定義一個獲取文章列表的標(biāo)簽,代碼如下
<?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'],
];
// 文章調(diào)用
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;'; // 在這里調(diào)用了另一個類,我們再定義一個類
$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標(biāo)簽就定義好,不過要想使用還需要在模板的配置下預(yù)加載標(biāo)簽

由于是多應(yīng)用模式,哪個應(yīng)用需要就在哪個應(yīng)用下面新建config目錄,新建view配置文件,把全局的view復(fù)制過來,加入一行,見上圖。

