Home > Backend Development > PHP Tutorial > thinkphp custom tags, view direct tag connection data_PHP tutorial

thinkphp custom tags, view direct tag connection data_PHP tutorial

WBOY
Release: 2016-07-13 17:47:46
Original
953 people have browsed it

First of all, you need to

Add a file named: TagLibZpass.class.php

to the ThinkPHPLibThinkTemplateTagLib directory

TagLibZpass.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

http://www.bkjia.com/PHPjc/478489.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478489.htmlTechArticleFirst, you need to add a file named: TagLibZpass.class.php TagLibZpass.class.php in the ThinkPHPLibThinkTemplateTagLib directory: ?php /* Zpass custom tag design by zpass 1685...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template