使ecshop模板中可引用常量

php相关 / 2014年03月12日 15时24分 / 10156人浏览
其实模板引擎原理上并不复杂,只是把一些模板标签替换为php中的函数,变量,语法结构罢了。 要在ecshop模板中加入引用常量的功能,只需在函数make_var()中加入几行代码 下面第24-26行为新加,这让就可在模板文件中通过 {$smarty.const.常量}来引用php中定义的常量了
/**
	 * 处理去掉$的字符串
	 *
	 * @access  public
	 * @param   string     $val
	 *
	 * @return  bool
	 */
	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;
	}