Thinkphp 模板引擎 if 嵌套层级过多的问题,嵌套3级就报错,取消层级限制

本文探讨了Thinkphp模板引擎在使用中遇到的if嵌套超过3层导致的错误问题,并指出了通过修改核心配置文件来取消嵌套层级限制的方法。

Thinkphp 模板引擎 if 嵌套层级过多的问题,嵌套3级就报错,取消层级限制

修改TP核心配置文件,路径如下

thinkphp/ThinkPHP/Library/Think/Template/TagLib/Cx.class.php
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace Think\Template\TagLib;
use Think\Template\TagLib;
/**
 * CX标签库解析类
 */
class Cx extends TagLib
{
   
   
    // 标签定义
    protected $tags = array(
        // 标签定义: attr 属性列表 close 是否闭合(0 或者1 默认1) alias 标签别名 level 嵌套层次
        'php'        => array(),
        'volist'     => array('attr' => 'name,id,offset,length,key,mod', 'level' => 3, 'alias' => 'iterate'),
        'foreach'    => array('attr' => 'name,item,key', 'level' => 3),
        'if'         => array('attr' => 'condition', 'level' => 2),
        'elseif'     => array('attr' => 'condition', 'close' => 0),
        'else'       => array('attr' => '', 'close' => 0),
        'switch'     => array('attr' => 'name', 'level' => 2),
        'case'       => array('attr' => 'value,break'),
        'default'    => array('attr' => '', 'close' => 0),
        'compare'    => array('attr' => 'name,value,type', 'level' => 3, 'alias' => 'eq,equal,notequal,neq,gt,lt,egt,elt,heq,nheq'),
        'range'      => array('attr' => 'name,value,type', 'level' => 3, 'alias' => 'in,notin,between,notbetween'),
        'empty'      => array('attr' => 'name', 'level' => 3),
        'notempty'   => array('attr' => 'name', 'level' => 3),
        'present'    => array('attr' => 'name', 'level' => 3),
        'notpresent' => array('attr' => 'name', 'level' => 3),
        'defined'    => array('attr' => 'name', 'level' => 3),
        'notdefined' => array('attr' => 'name', 'level' => 3),
        'import'     => array('attr' => 'file,href,type,value,basepath', 'close' => 0, 'alias' => 'load,css,js'),
        'assign'     => array('attr' => 'name,value', 'close' => 0),
        'define'     => array('attr' => 'name,value', 'close' => 0),
        'for'        => array('attr' => 'start,end,name,comparison,step', 'level' => 3),
    );
    /**
     * php标签解析
     * @access public
     * @param array $tag 标签属性
     * @param string $content  标签内容
     * @return string
     */
    public function _php($tag, $content)
    {
   
   
        $parseStr = '<?php ' . $content . ' ?>';
        return $parseStr;
    }
    /**
     * volist标签解析 循环输出数据集
     * 格式:
     * <volist name="userList" id="user" empty="" >
     * {user.username}
     * {user.email}
     * </volist>
     * @access public
     * @param array $tag 标签属性
     * @param string $content  标签内容
     * @return string|void
     */
    public function _volist($tag, $content)
    {
   
   
        $name  = $tag['name'];
        $id    = $tag['id'];
        $empty = isset($tag['empty']) ? $tag['empty'] : '';
        $key   = !empty($tag['key']) ? $tag['key'] : 'i';
        $mod   = isset($tag['mod']) ? $tag['mod'] : '2';
        // 允许使用函数设定数据集 <volist name=":fun('arg')" id="vo">{$vo.name}</volist>
        $parseStr = '<?php ';
        if (0 === strpos($name, ':')) {
   
   
            $parseStr .= '$_result=' . substr($name, 1) . ';';
            $name = '$_result';
        } else {
   
   
            $name = $this->autoBuildVar($name);
        }
        $parseStr .= 'if(is_array(' . $name . ')): $' . $key . ' = 0;';
        if (isset($tag['length']) && '' != $tag['length']) {
   
   
            $parseStr .= ' $__LIST__ = array_slice(' . $name . ',' . $tag['offset'] . ',' . $tag['length'] . ',true);';
        } elseif (isset($tag['offset']) && '' != $tag['offset']) {
   
   
            $parseStr .= ' $__LIST__ = array_slice(' . $name . ',' . $tag['offset'] . ',null,true);';
        } else {
   
   
            $parseStr .= ' $__LIST__ = ' . $name . ';';
        }
        $parseStr .= 'if( count($__LIST__)==0 ) : echo "' . $empty . '" ;';
        $parseStr 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值