ThinkPHP tag creation tutorial and thinkphp tag tutorial. ThinkPHP tag creation tutorial. thinkphp tag tutorial this article describes how to create ThinkPHP tags using examples. This article provides some reference for beginners or developers of ThinkPHP. ThinkPHP tag creation tutorial and thinkphp tag tutorial
This article describes how to create ThinkPHP labels using examples. It provides some reference for beginners or developers of ThinkPHP.
Generally, the default tag parser of ThinkPHP is in Lib/Template/TagLib/TagLibCx. class (version 2.1 is located in ThinkPHP/Lib/Think/Template/TagLib/TagLibCx. class. php), which defines commonly used ThinkPHP tags such as volist php.
Here we will add .
I. Tag format:
<{$cat.catname}>
II. Tag functions:
Loop output column where the parent class id is parentid
3. Procedure:
1. add the following to the private attributes 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, then combine the required php source code, and finally output the code on the page through echo.
The code is as follows:
Public function _ category ($ attr, $ content) {// resolve all attributes of a tag to the $ tag array. $ tag = $ this-> parseXmlAttr ($ attr, 'category '); // Get the attribute $ parentid = $ tag ['parentid'] in the tag; // define the variable $ result =! Empty ($ tag ['result'])? $ Tag ['result']: 'cat'; // defines the data query result to store the variable $ key =! Empty ($ tag ['key'])? $ Tag ['key']: 'I'; $ mod = isset ($ tag ['mod'])? $ Tag ['mod']: '2'; // you can use the CategoryModel encapsulated function $ SQL = "D ('Category ')-> "; $ SQL. = "getCategorys (". $ parentid. ')'; // piece together the output character $ 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 tag $ parsestr. =' <? Php endforeach; endif;?> '; Return $ parsestr ;}
The 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 (); // contains your own 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}>
This label is ready !! You can get rid of the volist to dynamically output what we want on the page!
A simple MVC-BLOG tutorial with ThinkPHP
There is a complete example of example on the official website. let's take a look at the manual.
How to use the thinkphp framework? Tutorial
Thinkphp is a small Framework. Generally, you know the mvc working mode and thinkphp will be ready soon.
This article explains how to create ThinkPHP labels using examples. It provides some reference for beginners or developers of ThinkPHP ....