据说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 }
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中定义的常量了