ThinkPHP2.1 detailed description of the import method for the custom tag library, thinkphp2.1 detailed description

Source: Internet
Author: User

ThinkPHP2.1 detailed description of the import method for the custom tag library, thinkphp2.1 detailed description

This topic describes how to import a custom tag library in thinkPHP2.1. We will share this with you for your reference. The details are as follows:

The TP manual seems to be unable to keep up with the pace, just a few words for the custom tag. After a long time, I finally imported the custom tag as follows:

1. Case: create a custom tag library class:@. Mylib. Tag. TagLibTest-the path of TP should be known.

Use the Tag demo file under Examples

<? Php // + keys // | ThinkPHP // + ---------------------------------------------------------------- // | Copyright (c) 2009 http://thinkphp.cn All rights reserved. // + certificate // | Licensed (http://www.apache.org/licenses/LICENSE-2.0) // + ------------------------------------------------------------------ // | Au Thor: liu21st <liu21st @ gmail.com = ""> // + ---------------------------------------------------------- // $ Id $ import ('taglib '); class TagLibArticle extends TagLib {// tag definition protected $ tags = array (// tag definition: // whether the attr attribute list close is closed (0 or 1 defaults to 1) alias tag alias level nested level 'Article' => array ('attr' => 'name, field, limit, order, where, SQL, key, mod ', 'level' => 3),); // defines the query database tag public function _ article ($ attr, $ content) {$ t Ag = $ this-> parseXmlAttr ($ attr, 'Article'); $ result =! Empty ($ tag ['result'])? $ Tag ['result']: 'Article'; // defines the variable for storing data query results $ key =! Empty ($ tag ['key'])? $ Tag ['key']: 'I'; $ mod = isset ($ tag ['mod'])? $ Tag ['mod']: '2'; if ($ tag ['name']) {// query condition $ SQL = "M ('{$ tag ['name']}')->"; $ SQL. = ($ tag ['field'])? "Field ({$ tag ['field']})->": ''; $ SQL. = ($ tag ['order'])? "Order ({$ tag ['order']})->": ''; $ SQL. = ($ tag ['where'])? "Where ({$ tag ['where']})->": ''; $ SQL. =" select () ";}else {if (! $ Tag ['SQL']) return ''; // exclude situations where no model name or SQL statement is specified. $ SQL. = "M ()-> query ('{$ tag [' SQL ']}')";} // concatenate the following output statement $ parsestr = '<? Php $ _ result = '. $ SQL. '; if ($ _ result): $ '. $ key. '= 0;'; $ parsestr. = 'foreach ($ _ result as $ key => $ '. $ result. '):'; $ parsestr. = '+ + $ '. $ key. '; $ mod = ($ '. $ key. '% '. $ mod. ');?> '; $ Parsestr. = $ content; // parse the content in the article tag $ parsestr. =' <? Php endforeach; endif;?> '; Return $ parsestr ;}}?> </Liu21st>

Create the taglibs. php file in the Conf directory of the project. The content is as follows:

<?phpreturn array( 'article'=>'@.TagLib.TagLibarticle',);?>

In this way, you can use the following in the template:

<TagLib name = "article"/> <! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN"> 

This is simple, but my idea is that the custom tag library can be imported automatically, in this way, you do not need to add tags like <tagLib name = "article"/> In the first line of each template.

This is just a problem.

2. automatically import the custom tag Library

Add the following to the config. php file:

'TAGLIB_PRE_LOAD' => 'article' ,

Clear cache and prompt error "instantiate a nonexistent class! ";

After several rounds of operations on the official TP forum, we found that there is only one solution: Use alias import, that is, add the import path under commonn/alias. php In the TP framework:

For example:

Copy codeThe Code is as follows: 'taglibarticle' => THINK_PATH. '/Lib/Think/Template/TagLib/TagLibArticle. class. php ',

There are also some schemes to modify the source code of the Template class, which is difficult to use-because I usually do not want to modify the core for a small problem.

So I thought about the import of Action:

Add import to your project base class BaseAction. class. php:

function _initialize() { import("@.Mylibs.Tag.TagLibArticle");}

Refresh cache, solve the problem...

This solution is simple and easy to use.

Supplement:

Using TP to automatically load configurations is faster:

'APP_AUTOLOAD_PATH'=> 'Think.Util.,@.Mylibs.Tag.',

Final Solution:

1. Create Mylibs. Tag. TagLibTest-custom Tag Library

2. Configuration:

'APP_AUTOLOAD_PATH'=> 'Think.Util.,@.Mylibs.Tag.','TAGLIB_PRE_LOAD' => 'test' ,

After the cache is deleted, it can be used normally.

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.