Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用自定义标签库提示错误 #29

Closed
hulang opened this issue Sep 9, 2024 · 1 comment
Closed

使用自定义标签库提示错误 #29

hulang opened this issue Sep 9, 2024 · 1 comment

Comments

@hulang
Copy link

hulang commented Sep 9, 2024

PHP版本:8.3.10
ThinkPHP版本:8.0.4
标签库:Cms.php

<?php

namespace app\common\taglib;

use think\facade\View;
use think\template\TagLib;
use hulang\tool\FileHelper;

class Cms extends TagLib
{
    /**
     * 定义标签列表
     */
    protected $tags = [
        // 标签定义: attr 属性列表 close 是否闭合(0 或者1 默认1) alias 标签别名 level 嵌套层次
        'select' => ['attr' => 'param,value', 'close' => 1],
        'radio' => ['attr' => 'param,value', 'close' => 1],
        'checkbox' => ['attr' => 'param,value', 'close' => 1],
    ];

    /**
     * 下拉选择组件
     */
    public function tagSelect($tag, $content)
    {
        // region|区域|江苏省:1,浙江省:2,山东省:3,湖北省:4,湖南省:5
        $param = trim($tag['param']);
        $paramArr = str2arr($param, '|');
        // 默认值
        $value = empty(trim($tag['value'])) ? 0 : trim($tag['value']);
        // 读取模版
        $path = join(DIRECTORY_SEPARATOR, [getStaticPath(0), 'tag', 'cms', 'select.html']);
        $template = FileHelper::readFile($path);
        // 解析数组
        $arr = str2arr($paramArr[2]);
        $list = [];
        foreach ($arr as $k => $v) {
            $dt = str2arr($v, ':');
            $list[$k]['name'] = $dt[0];
            $list[$k]['value'] = $dt[1];
            $list[$k]['checked'] = 0;
            if (trim($value) == trim($dt[1])) {
                $list[$k]['checked'] = 1;
            }
        }
        // 解析模版
        $data = [
            'value' => $value,
            'name' => $paramArr[0],
            'title' => $paramArr[1],
            'list' => $list,
        ];
        $html = View::display($template, $data);
        // 返回模板
        return $html;
    }

    /**
     * 单选按钮组件
     */
    public function tagRadio($tag, $content)
    {
        // gender|性别|男:1,女:2,不填:3
        $param = trim($tag['param']);
        $paramArr = str2arr($param, '|');
        // 默认值
        $value = empty(trim($tag['value'])) ? 0 : trim($tag['value']);
        // 读取模版
        $path = join(DIRECTORY_SEPARATOR, [getStaticPath(0), 'tag', 'cms', 'radio.html']);
        $template = FileHelper::readFile($path);
        // 解析数组
        $arr = str2arr($paramArr[2]);
        $list = [];
        foreach ($arr as $k => $v) {
            $dt = str2arr($v, ':');
            $list[$k]['name'] = $dt[0];
            $list[$k]['value'] = $dt[1];
            $list[$k]['checked'] = 0;
            if (trim($value) == trim($dt[1])) {
                $list[$k]['checked'] = 1;
            }
        }
        // 解析模版
        $data = [
            'value' => $value,
            'name' => $paramArr[0],
            'title' => $paramArr[1],
            'list' => $list,
        ];
        $html = View::display($template, $data);
        // 返回模板
        return $html;
    }

    /**
     * 复选框组件
     */
    public function tagCheckbox($tag, $content)
    {
        // area|行政区域|南京:1,北京:2,上海:3,深圳:4,广州:5
        $param = trim($tag['param']);
        $paramArr = str2arr($param, '|');
        // 默认值
        $value = empty(trim($tag['value'])) ? 0 : trim($tag['value']);
        // 读取模版
        $path = join(DIRECTORY_SEPARATOR, [getStaticPath(0), 'tag', 'cms', 'checkbox.html']);
        $template = FileHelper::readFile($path);
        // 解析数组
        $arr = str2arr($paramArr[2]);
        $list = [];
        foreach ($arr as $k => $v) {
            $dt = str2arr($v, ':');
            $list[$k]['name'] = $dt[0];
            $list[$k]['value'] = $dt[1];
            $list[$k]['checked'] = 0;
            if (trim($value) == trim($dt[1])) {
                $list[$k]['checked'] = 1;
            }
        }
        // 解析模版
        $data = [
            'value' => $value,
            'name' => $paramArr[0],
            'title' => $paramArr[1],
            'list' => $list,
        ];
        $html = View::display($template, $data);
        // 返回模板
        return $html;
    }
}

配置文件config/view.php

    // 自定义标签
    'taglib_pre_load' => 'app\common\taglib\Cms',

模版中调用:
{cms:select}{/cms:select}

提示错误:
模板标签错误:select

哪里我写错了?也不进入tagSelect方法中。。。没法调试。。。

@hulang
Copy link
Author

hulang commented Sep 13, 2024

已解决。。。

@hulang hulang closed this as completed Sep 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant