User-Defined tag library instance _ php instance in the ThinkPHP Group

Source: Internet
Author: User
This article mainly introduces how to customize the tag library under the ThinkPHP group and describes the specific steps of the custom tag library in the form of instances, which is of reference value, for more information about how to implement the custom tag library in the ThinkPHP group, see the example in this article. Share it with you for your reference. The details are as follows:

The custom tag library should be located in the ThinkPHP \ Extend \ Driver \ TagLib directory

1. Create a new tag class library named TagLibBlog. class. php.

2. Edit the new class library file, create a class, and inherit the TagLib parent class.

The Code is as follows:

Import ('taglib ');
Class TagLibBlog extends TagLib {
}


3. Declare a protected member variable $ tags in the class.

The Code is as follows:

Class TagLibBlog extends TagLib {
Protected $ tags = array (
'Mynav' => array ('attr' => 'limit, order', 'close' => 1)
/* Www.php.net
In the $ tags array, each cell, that is, each row, represents a tag, for example, {dede: article}. Here we also define a mynav,
The key name of each line of the tag is the tag name, and the key value is an array. The array contains two units. The first is attr, which is the attribute in the tag, separated by commas,
For example, num and typeid. If the value of close is 0, it is a non-closed tag. If the value of 1 is closed, it meansTo end
*/
);
}


4. After the tag is defined, we need to implement the tag function. Here we declare a function directly under the class file, starting with _, with the function name ending with the tag name mynav.

The Code is as follows:


Public function _ mynav ($ attr, $ content ){
$ Attr = $ this-> parseXmlAttr ($ attr );

Import ('@. Class. tool ');
$ Categories = M ('category ')-> limit ($ attr ['limit'])-> order ($ attr ['order'])-> select ();
$ Categories = Tool: formatMultiArray ($ categories );

$ Str = '';
For ($ I = 0; $ I $ Model = array (
'/[Field. id]/',
'/[Field. name]/'
);
$ Replace = array (
$ Categories [$ I] ['id'],
$ Categories [$ I] ['name']
);
$ Str. = preg_replace ($ model, $ replace, $ content );
}

Return $ str;
}


After defining tags, You need to load the tag library in the Project Public configuration file.
Config. php

The Code is as follows:

// Load the tag Library
'App _ AUTOLOAD_PATH '=>' @. TagLib ',
'Taglib _ BUILD_IN '=> 'cx, blog ',
The complete source code of the tag library is as follows:
<? Php
Import ('taglib ');
Class TagLibBlog extends TagLib {

Protected $ tags = array (
'Mynav' => array ('attr' => 'limit, order', 'close' => 1)
);

Public function _ mynav ($ attr, $ content ){
$ Attr = $ this-> parseXmlAttr ($ attr );

Import ('@. Class. tool ');
$ Categories = M ('category ')-> limit ($ attr ['limit'])-> order ($ attr ['order'])-> select ();
$ Categories = Tool: formatMultiArray ($ categories );

$ Str = '';
For ($ I = 0; $ I $ Model = array (
'/[Field. id]/',
'/[Field. name]/'
);
$ Replace = array (
$ Categories [$ I] ['id'],
$ Categories [$ I] ['name']
);
$ Str. = preg_replace ($ model, $ replace, $ content );
}

Return $ str;
}

}
?>


Test in template:

The Code is as follows:


[Field. name]

I hope this article will help you with ThinkPHP framework programming.

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.