thinkphp Custom Labels

Source: Internet
Author: User

The personal understanding of the label is that the PHP string is pieced together through Eval () to return the data. The process should be like this, add the definition label to <mytag:list></mytag> in the template, then in MVC, when parsing the template, use regular to match the label, if you find a matching label to find MyTag this class, and the instance calls the class under the The list method returns data by logical operation.

Extending a custom label in thinkphp

The following is an example of output breadcrumbs:

Refer to the official built-in label class notation. Build your tag class anywhere, I'm here to put the file name under the Home\mytag folder: class name +class.php

Here take Crumb.class.php as an example:

<?phpnamespace home\mytag;//in thinkphp the custom label class must use a namespace otherwise, thinkphp cannot find the class using Think\template\taglib;class crumb Extends taglib{    protected $tags   =  Array (        "show" = =  Array ()            );        Public Function _show ()    {
$str = ' <?php ';
$str. = ' echo ' haha haha haha "; ‘;
$str. = '?> ';
return $str;
}
}

The above has already defined the most basic class of custom tags, now as long as the need to use the label address to introduce the label, you can use the label <Crumb:show></Crumb:show> before you must first introduce the Label class and then call the custom label. The following code is introduced:

<taglib name= "Home\mytag\crumb"/>{/* This is the custom label class that needs to be loaded */}
<crumb:show></crumb:show>{/* Calling Custom tags */}

If the tag needs to be passed in as a parameter, the case is as follows:

Namespace Home\mytag;use think\template\taglib;class Crumb extends taglib{    protected $tags   =  Array (        "show"    = =  Array (' attr ' = ' Cateid ')            );    If you need to pass the value in the attribute mode, you must first define it here, otherwise it will not work properly. $content this does not need to be defined, as long as the input between the label can be obtained in this class to public    function _show ($tag, $content)    {        $str = ' <?php ';        $cateid = $tag [' Cateid '];        Here you run your own logic, output the content into a variable, and return to        $str. = ' echo '. <li><a href=\ "#\" > Home </a></li><li><a href=\ "#\" > News </a></li>< Li class=\ "active\" > This is a new information </li> '.        '; $str. = ' echo ' passed in via attributes: '. ' $cateid ". ' The contents of a passing in; '. ' $content ". '"; ';        $str. = '?> ';        return $str;    }}

Call this tag as follows:

<taglib name= "Home\mytag\crumb"/>
<crumb:show cateid= "" > here is the content can be passed to the $content variable </Crumb:show>

The results are as follows:

thinkphp Custom Labels

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.