thinkphp5 taglib custom tag tutorial

Learn to write for a while, finally done, by the way share!

taglib is the custom tag function of tp framework. If you have used cms, you must have seen something similar:

 

{dede:arclist typeid='' row='' col='' titlelen='' infolen=''  orderby='' keyword=''}
ssss...
{/dede:arclist}

 

Or:

 

{pc:content action="lists" cache="3600" num="20" page="$page"}
{/pc}

 

This kind of operation is very convenient for the development work,

So it's necessary to look at tp's taglib. The tutorial is as follows:

1. Create a new directory named taglib in common (I'm in common, you see, anyway, it's loaded with namespace)
2. In taglib, create Cc.php to inherit the Tglib of think. The code is as follows:

 

<?php
/**
 * Created by PhpStorm.
 * User: lichenchen
 * Date: 2018/3/25
 * Time: 8:34 p.m.
 */
namespace app\common\taglib;
use think\template\TagLib;
use app\common\model\Article;
class Cc extends TagLib
{


    protected $tags = [
        'articles' => ['attr'=> 'field,id,limit,cid,order,returnname', 'close'=>1]
    ];
    /**
     * Article Tags
     */
    public function tagArticles($tag, $content)
    {
        $id = $tag['id']?$tag['id']:'vo';
        $order         = empty($tag['order']) ? "'id DESC'" : '"'.$tag['order'].'"';
        $returnname = 'article_data';
        $field = "''";
        if (!empty($tag['field'])) {
            if (strpos($tag['field'], '$') === 0) {
                $field = $tag['field'];
                $this->autoBuildVar($field);
            } else {
                $field = "'{$tag['field']}'";
            }
        }
        $cid = "''";
        if (!empty($tag['cid'])) {
            if (strpos($tag['cid'], '$') === 0) {
                $cid = $tag['cid'];
                $this->autoBuildVar($cid);
            } else {
                $cid = "'{$tag['cid']}'";
            }
        }
        $limit = "''";
        if (!empty($tag['limit'])) {
            if (strpos($tag['limit'], '$') === 0) {
                $limit = $tag['limit'];
                $this->autoBuildVar($limit);
            } else {
                $limit = "'{$tag['limit']}'";
            }
        }else{
            $limit = '0,5';
        }
        $parse = <<<parse
<?php
    \$$returnname = \app\common\model\Article::taglib_articles([
    'field'   => {$field},
    'cid'=>{$cid},
    'limit'=>{$limit},
    'order'=>{$order}
]);
?>
{volist name="{$returnname}" id="{$id}"}
{$content}
{/volist}
parse;
        return $parse;
    }
}

 

Here is an article tag, articles

It calls the static method taglib_articles of the Article model, and pastes the code by the way, that is, it queries according to the conditions

 

public static function taglib_articles($param)
    {
        $field = $param['field'];
        $cid = $param['cid'];
        $limit = $param['limit'];
        $order = $param['order'];
        $result = Article::where('cid', 'in',$cid)->field($field)->limit($limit)->order($order)->select();
        return $result;
    }

 

After that, in the tp configuration file, add a configuration to load the custom label file

 

// +----------------------------------------------------------------------
    // |Template settings
    // +----------------------------------------------------------------------
    'template'               => [
        // Template engine type support php think Support extensions
        'type'         => 'Think',
        // Template path
        'view_path'    => '',
        // Template suffix
        'view_suffix'  => 'html',
        // Template filename separator
        'view_depr'    => DS,
        // Template engine normal label start tag
        'tpl_begin'    => '{',
        // Template engine normal label end tag
        'tpl_end'      => '}',
        // Label library label start tag
        'taglib_begin' => '{',
        // Label library label end tag
        'taglib_end'   => '}',
        'taglib_pre_load'     =>    'app\common\taglib\Cc', 
    ],

 

This is: 'taglib pre load' = > app\common\taglib\Cc '.


Then it can be used in the template

 

{cc:articles cid="1" field='id,title' limit="2" order="id asc" id="vo"}
<li><a href="#">{$vo.title}</a></li>
{/cc:articles}

 

It's very convenient. It took two hours to write it. I'd like to record it. I hope it can help my friends in need!

 

I've been watching the shuttle recently. I made a Fluent Chinese community , welcome to http://www.fluttercn.com

Keywords: PHP PhpStorm

Added by cobaswosa on Sat, 04 Apr 2020 22:21:17 +0300