ThinkPHP tag creation, ThinkPHP tag creation _ PHP Tutorial

Source: Internet
Author: User
Create ThinkPHP labels and ThinkPHP labels. ThinkPHP tag creation, ThinkPHP tag creation thinkphp default tag parser defines commonly used thinkphp tags such as volistphp in LibTemplateTagLibTagLibCx. class, ThinkPHP tag creation, ThinkPHP tag creation
The default tag parser of thinkphp defines common thinkphp tags such as volist php in Lib/Template/TagLib/TagLibCx. class. here I add Tag parsing label format: <{$ Cat. catname}> Label function: cyclically outputs the topic where the parent class id is parentid. in tagLibCx. add 'Category '= array ('attr' => 'parentid', level => 3) to the private attribute of the class. attr: the nested level of the attribute level tag of the tag. 2. the parsing principle of adding the parsing function tag is to get the corresponding information by reading the xml file, then combine the required php source code, and finally output the specific code on the page through echo:
 
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 =' $ Parsestr. = 'foreach ($ _ result as $ key => $ '. $ result .'):';
$ Parsestr. = '+ + $'. $ key. '; $ mod = ($'. $ key. '%'. $ mod. ');?> ';
$ Parsestr. = $ content; // Parse the content in the category label
$ Parsestr. =' ';
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 <{$ Cat. catname}> Such a label is ready. ^_^! You can get rid of the volist and dynamically output what we want on the page.

The default tag parser of thinkphp defines common thinkphp tags such as volist php in Lib/Template/TagLib/TagLibCx. class...

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.