(go) thinkphp template Custom Label usage method

Source: Internet
Author: User

This article mainly introduces the thinkphp template custom Label use method, the need for friends can refer to the next turn of the--http://www.jb51.net/article/51584.htm

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

Dedecms can be written as:

?
12345 <ul>{dede:arclist row=‘10‘orderby=‘id desc‘titlelen=‘‘}     <li>[field:title]</li>{/dede:arclist}</ul>

Phpcms can be written as:

?
1234567 <UL> {pc:content action= "hits" catid= "6" num= order= "views DESC" &NBSP;&NBSP;&NBSP; { Loop $data $r } &NBSP;&NBSP;&NBSP;&NBSP; <li>{ Code class= "PHP variable" > $r [TITLE]}</LI> {/loop} {/pc} </UL>

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

Naming conventions:

taglib+ Tag Library name. class.php

The following is an example of implementing call navigation as an illustration

The file TagLibNav.class.php is as follows:

?
1234567891011121314151617181920212223242526 <?phpclass TagLibNav extends TagLib {  //attr 属性列表   //close 是否闭合(0 或者1 默认1)  //alias 标签别名  //level 嵌套层次  // 标签定义如下:  protected $tags = array(    ‘nav‘ => array(‘attr‘ => ‘limit,order‘, ‘level‘ => 3,‘close‘=>1),  );  //定义查询数据库标签  //attr是属性列表,$content是存储标签之间的内容的  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;  }}?>

How HTML pages are called:

?
12345678910111213141516 <tagLib name="nav" />     //必须在头部进行引用否则会出错<html><head> <title>tablist</title></head><body>  <div class="nav">   <ul>    <li>首页</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:

?
12 ‘APP_AUTOLOAD_PATH‘=>‘@.TagLib‘//TagLib的位置  @.表示当前文件夹下‘TAGLIB_BUILD_IN‘=>‘Cx,Nav‘,       //Cx是thinkphp基础类库的名称必须引用否则volist等标签就无法使用,Nav是自己定义的标签名称

Controller:

?
1234567 <?phpclassIndexAction extends Action{  public functionindex() {    $this->display();  }}?>

A custom label is implemented at this point in the control

(go) thinkphp template Custom Label usage method

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.