Home > PHP Framework > ThinkPHP > Analyzing the use of ThinkPHP5.1 custom tags

Analyzing the use of ThinkPHP5.1 custom tags

藏色散人
Release: 2021-01-18 15:04:33
forward
3274 people have browsed it

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.

Analyzing the use of ThinkPHP5.1 custom tags1. 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 = &#39;<?php $map=[];&#39;;
        $parse .= &#39;$__LIST__ = Db::name(&#39;.$tableName.&#39;)->where(["cate_id"=>&#39;. $cateID .&#39;])
            ->limit(&#39;.$num.&#39;)
            ->select(); ?>&#39;;
        $parse .= &#39;{volist name="__LIST__" id="&#39;. $name .&#39;"}&#39;;
        $parse .= $content;
        $parse .= "{/volist}";
        return $parse;
    }
Copy after login

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}
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template