Using template tags can make website front-end development faster and simpler. People who have used dedecms, phpcms, and other content management systems should know that the front-end of cms uses template tags to call data. Take the list of called articles as an example:
Dedecms can be written as follows:
<ul>{dede:arclist row='10' orderby='id desc' titlelen=''} <li>[field:title]</li>{/dede:arclist}</ul>
Phpcms can be written as follows:
<ul>{pc:content action="hits" catid="6" num="10" order="views DESC"} {loop $data $r} <li>{$r[title]}</li> {/loop}{/pc}</ul>
ThinkPHP's custom labels can also implement such powerful functions. ThinkPHP custom tags are implemented through the TAG extension library. ThinkPHP itself comes with a tag extension library. As long as we inherit TagLib, we can define our own tags as needed.
Naming rules:
TagLib + tag library name. class. php
The following describes how to implement the call navigation.
The file TagLibNav. class. php is as follows:
<? Phpclass TagLibNav extends TagLib {// attr attribute list // whether close is closed (0 or 1 defaults to 1) // alias tag alias // level nested hierarchy // The label definition is as follows: protected $ tags = array ('nav' => array ('attr' => 'limit, order', 'level' => 3, 'close' => 1),); // define the query database tag // attr is the attribute list, and $ content is the public function _ nav ($ attr, $ content) {$ tag = $ this-> parseXmlAttr ($ attr, $ content); $ cate = M ('channel '); $ tb = $ cate-> order ($ tag ['order'])-> limit ($ tag ['limit'] )-> Select (); $ str = ''; for ($ I = 0; $ I <count ($ tb); $ I ++) {$ c = str_replace (array ("[filed: id]", "[filed: name]"), array ($ tb [$ I] ['id'], $ tb [$ I] ['name']), $ content); $ str. = $ c ;}return $ str ;}}?>
Html page call method:
<TagLib name = "nav"/> // the header must be referenced. Otherwise, an error occurs.
Configuration file:
'App _ AUTOLOAD_PATH '=> '@. tagLib ', // location of TagLib @. 'taglib _ BUILD_IN '=> 'cx, nav' in the current folder. // Cx is the name of the thinkphp base class library. Otherwise, tags such as volist cannot be used, nav is a custom label name.
Controller:
<?phpclass IndexAction extends Action{ public function index() { $this->display(); }}?>
So far, we have implemented custom tags and do not need to write a lot of code in the controller.