THINKPHP5 taglib Custom Label Tutorial

Source: Internet
Author: User

Learn to write a bit, finally fix, by the way share!

Taglib is a TP Framework custom label feature, if you've ever used a CMS, you've definitely seen something like:

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

Or:

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

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

So I think it is necessary to see the taglib of TP, the tutorial is as follows:

1, in common (I was in the common, you see, anyway, is the name space loaded) in the new directory Taglib
2, in Taglib new cc.php inherit think Tglib, the code is as follows:

<?PHP/** * Created by Phpstorm. * User:lichenchen * DATE:2018/3/25 * Time: PM 8:34*/namespace App\common\taglib; UseThink\template\taglib; Useapp\common\model\article;classCcextendstaglib{protected $tags= [        ' Articles ' = [' attr ' = ' field,id,limit,cid,order,returnname ', ' Close ' =>1]    ]; /** * Article tags*/     Public functionTagarticles ($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 tagged articles

Inside called the article model static method Taglib_articles, by the way also paste the code bar, is based on the condition query

 Public Static functionTaglib_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 supports PHP think support extensions' Type ' = ' Think ',//Template Path' View_path ' = ',//template suffix' View_suffix ' = ' html ',//template file name delimiter' View_depr ' = DS,//template engine normal tag start tag' Tpl_begin ' = ' {',//template engine normal tag end tag' Tpl_end ' = '} ',//Tag library tag start tag' Taglib_begin ' = ' {',//Tag library tag end tag' Taglib_end ' = ' '} ', ' taglib_pre_load ' = ' app\common\taglib\cc ',     ],

is: ' taglib_pre_load ' = ' app\common\taglib\cc ', this one.


Then you can use it in the stencil.

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

Very convenient, the whole two hours, just write well, hereby record, hope also can help to the needs of friends!

Recently watching flutter, engaged in a flutter Chinese community, you are welcome to see http://www.fluttercn.com

THINKPHP5 taglib Custom Label Tutorial

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.