Home > Article > PHP Framework > Analyzing the use of ThinkPHP5.1 custom tags
The following tutorial column will introduce you to the use of parsing ThinkPHP5.1 custom tags. I hope it will be helpful to friends in need!
Recently, there are projects that need to use custom tags. TP uses 5.1. Please note that 5.1 has major changes in the directory structure and procedures compared to 5.0. . Pay attention to your version. Let’s get to the point.1. Create the tag function file
Create the directory tabLib in the model directory, and create a Cms.php in the directory
namespace app\cms\tagLib; use think\Db; use think\template\TagLib; class Cms extends TagLib{ protected $tags = [ //标签定义: attr 属性列表,close 是否闭合(0或1,默认1),alias标签别名 level嵌套层次 'lists' => ['attr' => 'num,order,sort'], ]; // 当不使用content的时候,闭合标签没有效果 // 修改过此文件后,需要改动下模板的内容,否则模板有缓存不会执行新的内容。 public function tagLists($tag,$content){ $cateID = $tag['cate_id']; //栏目ID $num = $tag['num']; //数量 $order = input($tag['order']); //排序方式 $type = $cateID; $name = $tag['name']; $tableName = 'table_name'; $parse = '<?php $map=[];'; $parse .= '$__LIST__ = Db::name('.$tableName.')->where(["cate_id"=>'. $cateID .']) ->limit('.$num.') ->select(); ?>'; $parse .= '{volist name="__LIST__" id="'. $name .'"}'; $parse .= $content; $parse .= "{/volist}"; return $parse; }
Encountered doubts during development:
1. The above tagLists function corresponds to the tag definition in protected $tags. Pay attention to the capitalization, otherwise the function will not be found.
2. All characters in $parse are assembled strings. This string will be parsed again by TP's template tag, so you can use TP's original tags or native PHP statements in it. However, you cannot pass an external Array or object into $param as a parameter. You must know that it is just a string used for assembly. Please note the use of the $__LIST__ variable. If you want to read data from the database, you must write a complete PHP code. You cannot execute the query outside and then pass in the queried list as a variable.
2. Load this tag function in the template configuration information
Add a sentence in the module cms/config/template.php:
'taglib_pre_load' => 'app\cms\ tagLib\Cms',If you do not have this file, you can copy a template in the config directory in the root directory and put it in the cms/config directory. 3. Use tags in templates.
{cms:lists name="row" num="10" cate_id="5" order="1"} <li>{$row.title}</li> {/cms:lists}
Name, num, cate_id and other attributes will be passed in as the first parameter $tags of tagLists. The content between {cms:lists} and {$/cms:lists} will be passed into the second parameter of tagLists as a complete string $content.
Note: There are closed tags and non-closed tags here. If it is a closed tag, you must use content in (1). If it is a non-closed tag, you do not need to use it. If you do not use the content variable, the closed tag {/cms:lists} will be output as is and will not be parsed.
There is so much confusion for the time being, and the issue of paging lists is still being studied. I will add it after the study is completed.
Supplement: Do not perform data type conversion on the data obtained in the $tag[] array in tagList, because when a variable is passed, this variable will not take effect! ! So it is wrong to use intval($tag[num]) here.The above is the detailed content of Analyzing the use of ThinkPHP5.1 custom tags. For more information, please follow other related articles on the PHP Chinese website!