Home > php教程 > php手册 > thinkphp自定义标签,view直接标签连接数据

thinkphp自定义标签,view直接标签连接数据

WBOY
Release: 2016-06-13 10:46:17
Original
1199 people have browsed it

 

首先,需要在

ThinkPHP\Lib\Think\Template\TagLib\目录下增加一个文件名叫:TagLibZpass.class.php

TagLibZpass.class.php:

 

/*   Zpass自定义标签  design by zpass 16852868@qq.com,请别加QQ好友- -! */ 

class TagLibZpass extends TagLib{ 

    // 标签定义 

    protected $tags   =  array( 

        // 标签定义:attr 属性列表close 是否闭合(0 或者1 默认1)alias 标签别名level 嵌套层次 

        'select'=>array('attr'=>'table,limit,order,where,field','level'=>3), 

        ); 

    public function _select($attr,$content) 

    { 

        static $_iterateParseCache = array(); 

        //如果已经解析过,则直接返回变量值 

        $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);//去掉数组重复项 

        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 ; 

    } 

?> 

 

然后在视图上写上

调用的方法是

 

                        [field:title] 

                         

 

 

这个很容易理解吧www.2cto.com

我这个只是例子,代表可以自由扩展THINKPHP标签...做自己的特色标签

以上标签例子实际是查询classes表里的内容

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template