ThinkPHP template custom label usage _ PHP Tutorial

Source: Internet
Author: User
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  

  • Homepage
  • [Filed: name]

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.

...

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.