thinkphp Label Making Tutorial _php Example

Source: Internet
Author: User
In this paper, the author explains the method of making thinkphp label, and it can be used as a reference value for beginners or developers of thinkphp.

In general, the default label parser for thinkphp is located in the Lib/template/taglib/taglibcx.class (2.1 version thinkphp/lib/think/template/taglib/ TagLibCx.class.php), which defines commonly used thinkphp tags such as volist php.

Here we will add a tag parsing to this class .

First, the label format:

 
  
   
  <{$cat .catname}>
 
  

Second, the role of labelling:

Cyclic output parent class ID is the column of ParentID

Third, the operation steps:

1. In the private attribute of Taglibcx.class, add:

' Category ' =array (' attr ' = ' parentid ', level=>3)

Where attr: The nested hierarchy of the label's attribute level tag

2. Add an analytic function

The principle of the tag is to read the XML file to get the corresponding information, and then piece together into the required PHP source code, and finally through the echo on the page output.

The specific code is as follows:

Public Function _category ($attr, $content) {//parses all the properties of the tag into the $tag array $tag = $this->parsexmlattr ($attr, ' category ');// Get the attribute inside the tag $parentid= $tag [' ParentID '];//defines the page parsing variable $result =!empty ($tag [' result '])? $tag [' Result ']: ' Cat '; Define the result of data query $key =!empty ($tag [' key '])? $tag [' Key ']: ' i '; $mod = Isset ($tag [' mod '])? $tag [' MoD ']: ' 2 ';//Patchwork database query statements The Categorymodel encapsulated function is directly used here $sql = "D (' Category ')," $sql. = "Getcategorys (". $parentid. ') '; /Patchwork Output characters $parsestr = ' <?php $_result= '. $sql. '; if ($_result): $ '. $key. ' = 0; '; $parsestr. = ' foreach ($_result as $key =>$ '. $result. '): '; $parsestr. = ' ++$ '. $key. '; $mod = ($ '. $key. '% '. $mod. ');? > '; $parsestr. = $content;//Parse the contents of the category tag $parsestr. = ' <?php Endforeach; endif;? > '; return $parsestr;}

The Getcategorys method in Categorymodel:

/** Get column information according to ParentID * $parentid parent id* $withSelf whether to include their own */public function Getcategorys ($parentid, $withSelf =0) {$parentid = Intval ($parentid); $categorys = $this->where (Array (' parentid ' = $parentid, ' Ismenu ' =>1))->order (' Listorder ASC ')->select ()//contains own if ($withSelf) {$categorys 2= $this->where (array (' id ' = = $parentid, ' ismenu ' = >1))->limit (1)->select (); $category =array_merge ($categorys, $categorys 2);} return $categorys;}

3. References on the page:

 
  
   
  <{$cat .catname}>
 
  

Such a label is done!! You can get rid of that volist. Dynamically output what we want on the page!

  • 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.