The default tag parser of thinkphp is in Lib/Template/TagLib/TagLibCx. class.
It defines common thinkphp tags such as volist php.
Here, I add a <category> label resolution in this class.
Label format:
<Category parentid = '0'> <{$ cat. catname}> </category>
Tag function:
Loop output column where the parent class id is parentid
1. Add the private attribute of tagLibCx. class
'Category '= array ('attr' => 'parentid', level => 3)
Attr: attribute level of the tag. The nested level of the tag.
2. Add resolution Functions
The tag parsing principle is to get the corresponding information by reading the xml file, and then combine the required information.
Php source code, and finally output on the page through echo
The Code is as follows:
Public function _ category ($ attr, $ content)
{
// Resolve all attributes of the tag to the $ tag Array
$ Tag = $ this-> parseXmlAttr ($ attr, 'category ');
// Obtain the attributes in the tag
$ Parentid = $ tag ['parentid'];
// Define the page-Resolved Variables
$ Result =! Empty ($ tag ['result'])? $ Tag ['result']: 'cat'; // defines the data query result to store variables.
$ Key =! Empty ($ tag ['key'])? $ Tag ['key']: 'I ';
$ Mod = isset ($ tag ['mod'])? $ Tag ['mod']: '2 ';
// Pieces the database query statement. Here, the CategoryModel encapsulated function is used directly.
$ SQL = "D ('category ')-> ";
$ SQL. = "getCategorys (". $ parentid .')';
// Concatenate 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 content in the category label
$ Parsestr. = '<? Php endforeach; endif;?> ';
Return $ parsestr;
}
GetCategorys method in CategoryModel
/*
* Obtain the topic information based on parentid.
* $ Parentid parent id
* $ Whether withSelf contains itself
*/
Public function getCategorys ($ parentid, $ withSelf = 0)
{
$ Parentid = intval ($ parentid );
$ Categorys = $ this-> where (array ('parentid' => $ parentid, 'ismenu '=> 1)-> order ('listorder ASC ') -> select ();
// Include yourself
If ($ withSelf)
{
$ Categorys2 = $ this-> where (array ('id' => $ parentid, 'ismenu '=> 1)-> limit (1)-> select ();
$ Category = array_merge ($ categorys, $ categorys2 );
}
Return $ categorys;
}
3. Reference on the page
<Category parentid = '0'>
<{$ Cat. catname}>
</Category>
Such a label is ready. ^_^! You can get rid of the volist and dynamically output what we want on the page.
From wok