Home >Backend Development >PHP Tutorial >thinkphp custom tags, view direct tag connection data_PHP tutorial
First of all, you need to
Add a file named: TagLibZpass.class.php
to the ThinkPHPLibThinkTemplateTagLib directoryTagLibZpass.class.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: attr attribute list close whether it is closed (0 or 1, default 1) alias tag alias level nesting level
'select'=>array('attr'=>'table,limit,order,where,field','level'=>3),
);
Public function _select($attr,$content)
{
static $_iterateParseCache = array();
//If it has been parsed, return the variable value 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 array duplicates
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
on the view
The method called is
[field:title]< /span>
This is easy to understand, right www.2cto.com
This is just an example, it means you can freely extend the THINKPHP tag...make your own special tag
The above tag example actually queries the contents of the classes table