Thinkphp custom tag, view direct tag connection data

Source: Internet
Author: User

 

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.