ThinkPHP template custom label usage method. Using template tags can make website front-end development faster and easier. people who have used dedecms, phpcms, and other content management systems should know that, the front-end of cms uses Template tags to call and use template tags to make website front-end development faster and easier. people who have used dedecms, phpcms, and other content management systems should know that, the front-end of cms uses Template labels to call data. Take the list of called articles as an example:
Dedecms can be written as follows:
{dede:arclist row='10' orderby='id desc' titlelen=''}
- [field:title]
{/dede:arclist}
Phpcms can be written as follows:
{pc:content action="hits" catid="6" num="10" order="views DESC"} {loop $data $r}
- {$r[title]}
{/loop}{/pc}
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
Html page call method:
// Must be referenced in the header; otherwise, an error occurs. Tablist
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.
...