筆記:使ecshop 模板中可引用 常量
據說ecshop的模板類是修改的smarty,不過個人感覺不是修改是完全重寫了。它和smarty上只是模板標簽上有相同的地方,同時閹割了很多功能。
比如$smarty.const.'常量',這個就不能用。
其實模板引擎原理上并不復雜,只是把一些模板標簽替換為php中的函數,變量,語法結構罷了。
這次要在ecshop模板中加入引用常量的功能,只需在函數make_var()中加入兩行代碼
1 function make_var($val)
2 {
3 if (strrpos($val, '.') === false)
4 {
5 if (isset($this->_var[$val]) && isset($this->_patchstack[$val]))
6 {
7 $val = $this->_patchstack[$val];
8 }
9 $p = '$this->_var[\'' . $val . '\']';
10 }
11 else
12 {
13 $t = explode('.', $val);
14 $_var_name = array_shift($t);
15 if (isset($this->_var[$_var_name]) && isset($this->_patchstack[$_var_name]))
16 {
17 $_var_name = $this->_patchstack[$_var_name];
18 }
19 if ($_var_name == 'smarty')
20 {
21 if($t[0] == 'const'){
22 return strtoupper($t[1]);
23 }
24 $p = $this->_compile_smarty_ref($t);
25 }
26 else
27 {
28 $p = '$this->_var[\'' . $_var_name . '\']';
29 }
30 foreach ($t AS $val)
31 {
32 $p.= '[\'' . $val . '\']';
33 }
34 }
35
36 return $p;
37 }
其中21-23行是新加的,這讓就可在模板文件中通過 {$smarty.const.常量}來引用php中定義的常量了
浙公網安備 33010602011771號