Thinkphp5 taglib custom label tutorial, thinkphp5taglib
I learned to write it and finally got it done. By the way, let's share it!
Taglib is the custom tag function of the tp framework. If you have used cms, you must have seen it like:
{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 operation is convenient for development,
So I think it is necessary to take a look at the taglib of tp. The tutorial is as follows:
1. Create a directory named taglib in common (I am in common, as you can see, it is loaded using a namespace ).
2. Create Cc. php In taglib to inherit think's Tglib. The Code is as follows:
<? Php/*** Created by PhpStorm. * User: lichenchen * Date: 2018/3/25 * Time: */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 tag */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 label articles.
The static method taglib_articles of the Article model is called in it. By the way, I also paste the code, which is based on the condition query.
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; }
Then, add a configuration in the tp configuration file to load
// + Templates // | template settings // + templates 'template' => [// The template engine type supports php think extension 'type' => 'think ', // template path 'view _ path' => '', // template suffix 'view _ suffix '=> 'html ', // The template file name delimiter 'view _ depr' => DS, // The standard tag of the template engine starts to mark 'tpl _ in '=> '{', // The template engine standard tag end tag 'tpl _ end' => '}', // The tag library tag start tag 'taglib _ in in' => '{', // tag library tag end tag 'taglib _ end' => '}', 'taglib _ pre_load '=> 'app \ common \ taglib \ CC',],
That is, 'taglib _ pre_load '=> 'app \ common \ taglib \ CC.
Then you can use it 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 only two hours to write it. I 'd like to record it and hope it can help the friends who need it!
Recently watch flutter, engaged in a flutter Chinese community, welcome to look at http://www.fluttercn.com