thinkphp Template Custom Label use method _php instance

Source: Internet
Author: User
Tags php template smarty template

The use of template tags can make the site foreground development faster and simpler, the use of Dedecms, PHPCMS and other content management system should know that the CMS front desk is to use template tags to invoke the data. To invoke the list of articles as an example:

Dedecms can be written as:

<ul>
{dede:arclist row= ' orderby= ' id desc ' titlelen= '}
   <li>[field:title]</li>
{/ Dede:arclist}
</ul>

Phpcms can be written as:

<ul>
{pc:content action= "hits" catid= "6" num= "ten" order= "Views DESC"}
  {loop $data $r}
  <li>{$r [Title]} </li>
  {/loop}
{/pc}
</ul>

Thinkphp's custom tags are also capable of achieving such powerful functionality. Thinkphp custom tags are implemented through the tag extension library. and thinkphp itself with a tag extension library as long as we inherit the taglib will be able to meet the definition of their own label.

Naming conventions:

taglib+ Tag Library name. class.php

Here's how to implement call navigation as an example

The document TagLibNav.class.php as follows:

<?php
class Taglibnav extends TagLib {
 //attr property list 
 //close closed (0 or 1 default 1)
 //alias label alias
 //level Nesting levels
 //tags are defined as follows:
 protected $tags = Array (
  ' nav ' => array (' attr ' => ' limit,order ', ' Level ' => 3, ' close ' =>1),
 );
 Define Query Database label
 //attr is a list of properties, $content is the public
 function _nav ($attr, $content) {$tag = $this that stores the contents of the label
  -> 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;
 }
? >

How HTML pages are invoked:

<taglib name= "nav"/>   //Must be referenced on the head otherwise there will be an error
 
 

Configuration file:

' App_autoload_path ' => ' @. TagLib ',//taglib position @. Represents the
' taglib_build_in ' => ' Cx,nav ' under the current folder,    // CX is thinkphp the name of the base class library must be referenced otherwise volist and so on can not be used, NAV is the label name that you define

Controller:

<?php
class Indexaction extends action{public
 function Index () {
  $this->display ();
 }
}
?>

Now that you've implemented a custom label, you don't have to write a lot of code in the controller.

More interested in thinkphp related content readers can view the site topics: "thinkphp Introductory Course", "thinkphp Template Operation Skills Summary", "thinkphp Common Methods Summary", "Smarty Template Introductory Course" and "PHP template technology Summary."

I hope this article will help you with the PHP program design based on thinkphp framework.

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.