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!