ThinkPHP Template Custom Tag Usage

  • 2021-07-02 23:50:09
  • OfStack

Using template tags can make website foreground development faster and simpler. People who have used dedecms, phpcms and other content management systems should know that cms foreground uses template tags to call data. Take the list of calling articles as an example:

dedecms can be written as:


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

phpcms can be written as:


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

ThinkPHP's custom tags can also achieve such powerful functions. ThinkPHP custom tags are implemented through the TAG extension library. ThinkPHP itself comes with an tag extension library. As long as we inherit TagLib, we can define our own labels at will.

Naming convention:

TagLib + tag library name. class. php

The following is an example of implementing call navigation

The file TagLibNav. class. php is as follows:


<?php
class TagLibNav extends TagLib {
 //attr  Attribute list  
 //close  Is it closed ( 0  Or 1  Default 1 ) 
 //alias  Label alias 
 //level  Nested hierarchy 
 //  Labels are defined as follows: 
 protected $tags = array(
  'nav' => array('attr' => 'limit,order', 'level' => 3,'close'=>1),
 );
 // Define query database labels 
 //attr Is a list of attributes, $content That 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 page call mode:


<tagLib name="nav" />   // Must be referenced in the header or an error will occur 
<html>
<head>
 <title>tablist</title>
</head>
<body>
 <div class="nav">
  <ul>
  <li> Home page </li>
  <nav:nav limit='4' order='id asc'>
   <li><a href="[filed:id]">[filed:name]</a></li>
  </nav:nav>
  </ul>
 </div>
 </body>
</html>

Profile:


'APP_AUTOLOAD_PATH'=>'@.TagLib', //TagLib Location of  @. Indicates that under the current folder 
'TAGLIB_BUILD_IN'=>'Cx,Nav',    //Cx Yes thinkphp The name of the underlying class library must be referenced otherwise volist When the label can't be used, Nav Is a self-defined label name 

Controller:


<?php
class IndexAction extends Action{
 public function index() {
  $this->display();
 }
}
?>

At this point, the custom tag is realized, and there is no need to write a lot of code in the controller.

Readers who are interested in thinkPHP can check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "smarty Template Introduction Basic Tutorial" and "PHP Template Technology Summary".

I hope this article is helpful to the PHP programming based on ThinkPHP framework.


Related articles: