First, you must
Add a file name under the ThinkPHP \ Lib \ Think \ Template \ TagLib \ directory: TagLibZpass. class. php
TagLibZpass. class. php:
<? Php
/* Zpass custom label design by zpass 16852868@qq.com, please do not add QQ friends --! */
Class TagLibZpass extends TagLib {
// Tag Definition
Protected $ tags = array (
// Tag definition: whether to close the attr attribute list (0 or 1 by default). alias tag alias level nested hierarchy
'Select' => array ('attr' => 'table, limit, order, where, field', 'level' => 3 ),
);
Public function _ select ($ attr, $ content)
{
Static $ _ iterateParseCache = array ();
// If it has been parsed, the variable value is returned directly.
$ CacheIterateId = md5 ($ attr. $ content );
If (isset ($ _ iterateParseCache [$ cacheIterateId])
Return $ _ iterateParseCache [$ cacheIterateId];
$ Tag = $ this-> parseXmlAttr ($ attr, 'table ');
$ Db = M ($ tag ['table']);
$ List = $ db-> where ($ tag ['where'])-> order ($ tag ['order']) -> limit ($ tag ['limit'])-> field ($ tag ['field'])-> select ();
Preg_match_all ("/\ [field \: \ w * \]/", $ content, $ match );
$ Match = $ match [0];
$ Match = array_unique ($ match); // remove the array repeated items.
Foreach ($ match as $ I =>$ vo ){
$ Field [$ I] = substr ($ vo, 7, count ($ vo)-2 );
}
Foreach ($ list as $ I =>$ value ){
If ($ I = 0 ){
$ ParseStr = $ content;
} Else {
$ ParseStr = $ parseStr. $ content;
}
Foreach ($ match as $ j => $ vo ){
$ ParseStr = str_replace ($ vo, $ value [$ field [$ j], $ parseStr );
}
}
$ _ IterateParseCache [$ cacheIterateId] = $ parseStr;
If (! Emptyempty ($ parseStr )){
Return $ this-> tpl-> parse ($ parseStr );
}
Return;
}
}
?>
Then write in the view
<Taglib name = 'zpass'/>
The call method is
<Zpass: select table = "classes" order = "sort desc" field = "id, title" limit = "3">
<Span class = "marginleft_1"> <a href = '{: U ("News/index? Id = [field: id] ")} '> [field: title] </a> </span>
</Zpass: select>
This is easy to understand. www.2cto.com
This is just an example. It means that THINKPHP labels can be freely expanded...
The preceding tag example is used to query the content in the classes table.