Home  >  Article  >  Backend Development  >  ThinkPHP label production_PHP tutorial

ThinkPHP label production_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:47:31974browse

thinkphp’s default tag parser is in Lib/Template/TagLib/TagLibCx.class

It defines commonly used volist php and other commonly used thinkphp tags

Here the author adds a tag parsing

to this class

Tag format:

<{$cat.catname}>

Tag function:

Loop out the column whose parent ID is parentid

1. Add

in the private attribute of tagLibCx.class

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

Among them, attr: the attribute level of the tag and the nesting level of the tag

2. Add parsing function

The principle of tag parsing is to obtain the corresponding information by reading the xml file, and then piece it together into the required

php source code, finally output on the page through echo

The specific code is as follows:

public function _category($attr,$content)

{

//Parse all attributes of the tag into the $tag array

$tag = $this->parseXmlAttr($attr,'category');

//Get the attributes in the tag

$parentid= $tag['parentid'];

//Define variables for page parsing

$result = !empty($tag['result'])?$tag['result']:'cat'; //Define data query result storage variables

$key = !empty($tag['key'])?$tag['key']:'i';

$mod = isset($tag['mod'])?$tag['mod']:'2';

//Piece together the database query statement. Here we directly use the function encapsulated by CategoryModel

$sql = "D('Category')->";

$sql .= "getCategorys(".$parentid.')';

//Piece together the output characters

$parsestr = '

$parsestr .= 'foreach($_result as $key=>$'.$result.'):';

$parsestr .= '++$'.$key.';$mod = ($'.$key.' % '.$mod.' );?>';

$parsestr .= $content;//Parse the content in the category tag

$parsestr .= '';

return $parsestr;

}

getCategorys method in CategoryModel

/*

* Get column information based on parentid

* $parentid parent id

* Whether $withSelf contains self

*/

public function getCategorys($parentid,$withSelf=0)

{

$parentid=intval($parentid);

$categorys=$this->where(array('parentid'=>$parentid,'ismenu'=>1))->order('listorder ASC')->select();

//include self

if($withSelf)

{

$categorys2=$this->where(array('id'=>$parentid,'ismenu'=>1))->limit(1)->select();

$category=array_merge($categorys,$categorys2);

}

return $categorys;

}

3. Quotes on the page

<{$cat.catname}>

Such a tag is ready^_^! We can get rid of the volist and dynamically output what we want on the page

Excerpted from Wok

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478508.htmlTechArticlethinkphp’s default tag parser defines the commonly used volist php in Lib/Template/TagLib/TagLibCx.class For commonly used thinkphp tags, the author adds a catego to this class...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn