Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn about custom tag small instances.
For details about how to learn custom tags, see the introduction in the Manual (extension> tag library ).
Http://document.thinkphp1.cn/manual_3_2.html#taglib_driver
There are three main steps:
1. custom tag (new Test. class. php file)
Namespace Think \ Template \ TagLib; // directory of the current file
Use Think \ Template \ TagLib; // The TagLib. class. php file in the Template directory
Class Test extends TagLib {
Protected $ tags = array (
// Define tags
'Edit' => array ('attr' => 'Type, name, id, value', 'close' => 0), // call this tag, it is mapped to the real input tag (because the input tag is returned), but here it is changed to a name.
'Content' => array ('attr' => 'name, id'), // Same principle as above
);
// Edit Tag resolution (the "_" prefix must be added for each tag resolution method when defining)
Public function _ edit ($ tag ){
$ Name = $ tag ['name'];
$ Id = $ tag ['id'];
$ Type = $ tag ['type'];
$ Value = $ tag ['value'];
$ Str ="";
Return $ str;
}
// Content Tag Parsing
Public function _ content ($ tag, $ content ){
$ Name = $ tag ['name'];
$ Id = $ tag ['id'];
$ Str =''. $ Content .'';
Return $ str;
}
}Ii. assign values to variables and output templates
public function index(){
$this->assign('value', 'hello world !');
$this->display('tag');
}3. Output in the template
{$ Value}
Automatic call:
Add the following configuration to the config file:
'TAGLIB_LOAD'=>true,
'APP_AUTOLOAD_PATH'=>'@.TagLib',
'TAGLIB_BUILD_IN'=>'Cx,Test',Then the template output will be written like this (test: prefix and ):
{$value} Effect:
In a simple understanding, the so-called custom tag is to see what is returned. If the returned html Tag is, it is displayed as an html Tag. If the returned result is pure data, then it is displayed in the form of text (multiple pieces of data are collected from SQL)
Protected $ tags = array (
'Data' => array ('attr' => 'type', 'close' => 0), // input tag
);
Public function _ data ($ tag ){
Switch ($ tag ['type']) {
Case 1:
Return'Type 1 is returned.
';
Break;
Case 2:
Return'Type 1 is returned.
';
Break;
Default:
Return'Default type
';
Break;
}
}Template rendering:
Effect:
Thank you for your attention ~
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB