Thinkphp custom tag

Source: Internet
Author: User
With the template tag, website front-end development is faster and simpler, saving time for the work originally completed by programmers. now, as long as you understand html code, you can build your own website, anyone who has been in contact with dedecms, phpcms, and other content management systems knows this.

With the template tag, website front-end development is faster and simpler, saving time for the work originally completed by programmers. now, as long as you understand html code, you can build your own website, anyone who has been familiar with dedecms, phpcms, and other content management systems knows that the front-end of cms uses Template tags to call data, such as calling the document list.

Dedecms:

    • {Dede: arclist row = '10' orderby = 'Id desc 'titlelen = ''}
    • [Field: title]
    •  
    • {/Dede: arclist}
     
  1. Phpcms:
    • {Pc: content action = "hits" catid = "6" num = "10" order = "views DESC "}
    • {Loop $ data $ r}
    • {$ R [title]}
    •  
    • {/Loop}
    • {/Pc}
     

This is their strength. next we will introduce THINKPHP's powerful TAG Extension Library. thinkphp comes with a tag Extension Library. as long as we inherit TagLib, we can define our own tags as we like.

Naming rules:

TagLib + tag library name. class. php

The following example shows how to implement the call navigation. the file name is TagLibNav. class. php.

  1. Class TagLibNav extends TagLib {
  2. // Whether to close the attr attribute list (0 or 1 by default). alias tag alias level nested hierarchy
  3. // Tag definition
  4. Protected $ tags = array (
  5. 'Nav' => array ('attr' => 'Limit, Order', 'level' => 3, 'close' => 1 ),
  6. );
  7.  
  8. // Define the query database tag
  9. // Attr is the attribute list and $ content stores the content between tags.
  10. Public function _ nav ($ attr, $ content ){
  11. $ Tag = $ this-> parseXmlAttr ($ attr, $ content );
  12. $ Cate = M ('channel ');
  13. $ Tb = $ cate-> order ($ tag ['order'])-> limit ($ tag ['limit'])-> select ();
  14. $ Str = '';
  15. For ($ I = 0; $ I < li><>
  16. {
  17. $ C = str_replace (array ("[filed: id]", "[filed: name]"), array ($ tb [$ I] ['id'], $ tb [$ I] ['name']), $ content );
  18. $ Str. = $ c;
  19. }
  20. Return $ str;
  21. }
  22. }
  23. ?>

Html call method:

  1. // Must be referenced in the header; otherwise, an error occurs.
  2.  
  3.  
  4.  Tablist 
  5.  
  6.  
    • Homepage
    •  
    • [Filed: name]
    •  
    •  
     
  7.  
  8.   
  9.  

Configuration file

'App _ AUTOLOAD_PATH '=>' @. TagLib ', // The location of TagLib @. indicates the location in the current folder

'Taglib _ BUILD_IN '=> 'cx, Nav', // Cx is the name of the thinkphp Base Class Library, which must be referenced; otherwise, tags such as volist cannot be used, and Nav is the name of the custom label.

Controller:

  1. Class IndexAction extends Action {
  2. Public function index (){
  3. $ This-> display ();
  4. }
  5. }
  6. ?>

In this way, you don't need to write a lot of code in the controller. Is it easy?

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.