<button id="g80sy"></button>
  • <abbr id="g80sy"></abbr>
  • <button id="g80sy"></button>
    <rt id="g80sy"><delect id="g80sy"></delect></rt>
  • tp6多應(yīng)用自定義taglib標簽

    程序猿 2021-03-26 09:28:42 9166瀏覽 加載中

    1,首先在app下面新建common文件夾做為公共文件夾。

    2,在common下面新建taglib來放我們的標簽

    3,新建一個類如ht,注意需要繼承think\template\TagLib;

    <?php
    
    
    namespace app\common\taglib;
    
    
    use think\template\TagLib;
    
    class ht extends TagLib
    {
    
    }

    代碼解構(gòu):

    自定義標簽.jpg

    假如我們要定義一個獲取文章列表的標簽,代碼如下

    <?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標簽就定義好,不過要想使用還需要在模板的配置下預(yù)加載標簽

    自定義標簽2.jpg

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

    標簽: tp6 taglib
    最后修改:2025-03-23 19:24:57

    非特殊說明,本博所有文章均為博主原創(chuàng)。

    主站蜘蛛池模板: 綦江县| 湖州市| 甘南县| 若尔盖县| 连城县| 天水市| 孟州市| 玛多县| 庆安县| 南靖县| 夏邑县| 吉林省| 大足县| 尉氏县| 双辽市| 泊头市| 西昌市| 高尔夫| 砀山县| 宁强县| 万源市| 闵行区| 东方市| 巴青县| 阿坝县| 锦屏县| 渝中区| 习水县| 旌德县| 两当县| 黄山市| 北海市| 卢氏县| 汕尾市| 砀山县| 五家渠市| 和顺县| 黎川县| 周宁县| 呼玛县| 景宁|