使ecshop 模板中可引用 常量

这篇很早前在博客园写的,现在稍微修改下转过来

据说ecshop的模板类是修改的smarty,不过个人感觉不是修改是完全重写了。它和smarty上只是模板标签上有相同的地方,同时阉割了很多功能。

比如$smarty.const.’常量’,这个就不能用。(其实在模版的有些地方可以直接使用常量,比如在判断语句中,但是你若想输出常量值就不是很方便了)

其实模板引擎原理上并不复杂,只是把一些模板标签替换为php中的函数,变量,语法结构罢了。

这次要在ecshop模板中加入引用常量的功能,只需在函数 cls_template.php中的 make_var()中加入两行代码

 

function make_var($val)
    {
        if (strrpos($val, '.') === false)
        {
            if (isset($this->_var[$val]) && isset($this->_patchstack[$val]))
            {
                $val = $this->_patchstack[$val];
            }
            $p = '$this->_var[\'' . $val . '\']';
        }
        else
        {
            $t = explode('.', $val);
            $_var_name = array_shift($t);
            if (isset($this->_var[$_var_name]) && isset($this->_patchstack[$_var_name]))
            {
                $_var_name = $this->_patchstack[$_var_name];
            }
            if ($_var_name == 'smarty')
            {    
                if($t[0] == 'const'){#此处为新添加
                    return strtoupper($t[1]);
                }
                 $p = $this->_compile_smarty_ref($t);
            }
            else
            {
                $p = '$this->_var[\'' . $_var_name . '\']';
            }
            foreach ($t AS $val)
            {
                $p.= '[\'' . $val . '\']';
            }
        }

        return $p;
    }

其中21-23行是新加的,这让就可在模板文件中通过 {$smarty.const.常量}来引用php中定义的常量了

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注