Thinkphp custom tag learning notebook

Source: Internet
Author: User

Dedecms:

The code is as follows: Copy code
<Ul>
{Dede: arclist row = '10' orderby = 'Id desc 'titlelen = ''}
<Li> [field: title] </li>
{/Dede: arclist}
</Ul>
Phpcms:
<Ul>
{Pc: content action = "hits" catid = "6" num = "10" order = "views DESC "}
{Loop $ data $ r}
<Li >{$ r [title]} </li>
{/Loop}
{/Pc}
</Ul>

This is what makes them powerful. Next we will introduce THINKPHP's powerful TAG Extension Library. Thinkphp comes with a tag Extension Library. As long as we inherit TagLib, we can define our own tags as needed.
Naming rules: www.111cn.net
TagLib + tag library name. class. php
The following example shows how to call the navigation file name TagLibNav. class. php.

The code is as follows: Copy code

<? Php
Class TagLibNav extends TagLib {
// Whether to close the attr attribute list (0 or 1 by default). alias tag alias level nested hierarchy
// Tag definition
Protected $ tags = array (
      
'Nav' => array ('attr' => 'Limit, Order', 'level' => 3, 'close' => 1 ),
);

// Define the query database tag www.111cn. Net
// Attr is the attribute list and $ content stores the content between tags.
Public function _ nav ($ attr, $ content ){
$ Tag = $ this-> parseXmlAttr ($ attr, $ content );
$ Cate = M ('channel ');
$ Tb = $ cate-> order ($ tag ['order'])-> limit ($ tag ['limit'])-> select ();
$ Str = '';
For ($ I = 0; $ I <count ($ tb); $ I ++)
        {
$ C = str_replace (array ("[filed: id]", "[filed: name]"), array ($ tb [$ I] ['id'], $ tb [$ I] ['name']), $ content );
$ Str. = $ c;
        }
Return $ str;
    }
}
?>

Html call method

The code is as follows: Copy code
<TagLib name = "nav"/> // The header must be referenced. Otherwise, an error occurs.
<Html>
<Head>
<Title> tablist </title>
</Head>
<Body>
<Div class = "nav">
<Ul>
<Li> homepage </li>
<Nav: nav limit = '4' order = 'Id asc '>
<Li> <a href = "[filed: id]"> [filed: name] </a> </li>
</Nav: nav>
</Ul>
</Div>
</Body>
</Html>

Configuration file
'App _ AUTOLOAD_PATH '=>' @. TagLib ', // The location of TagLib @. Indicates the location in the current folder
'Taglib _ BUILD_IN '=> 'cx, Nav', // Cx is the name of the thinkphp Base Class Library, which must be referenced; otherwise, tags such as volist cannot be used, and Nav is the name of the custom label.
Controller

The code is as follows: Copy code
<? Php
Class IndexAction extends Action {
Public function index (){
$ This-> display ();
    }
}
?>

In this way, you don't need to write a lot of code in the controller !! Is it easy?

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.