Getting started with custom tags

Source: Internet
Author: User
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

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.