让ecshop模板支持php运算

让ecshop模板支持php运算在 cls_template.php 底部加入函数:

/**
* 处理if标签
*
* @access public
* @param string $tag_args
* @param bool $elseif
*
* @return string
*/
function _compile_math_tag($tag_args)
{
preg_match_all('/\-?\d+[\.\d]+|\'[^\'|\s]*\'|"[^"|\s]*"|[\$\w\.]+|!==|===|==|!=|<>|<<|>>|<=|>=|&&|\|\||\(|\)|,|\!|\^|=|&|<|>|~|\||\%|\+|\-|\/|\*|\@|\S/', $tag_args, $match);

$tokens = $match[0];
// make sure we have balanced parenthesis
$token_count = array_count_values($tokens);
if (!empty($token_count['(']) && $token_count['('] != $token_count[')'])
{
// $this->_syntax_error('unbalanced parenthesis in if statement', E_USER_ERROR, __FILE__, __LINE__);
}

for ($i = 0, $count = count($tokens); $i < $count; $i++)
{
$token = &$tokens[$i];
switch (strtolower($token))
{
case 'add':
$token = '+';
break;
case 'sub':
$token = '-';
break;

case 'div':
$token = '/';
break;

case 'mak':
$token = '*';
break;

default:
if ($token[0] == '$')
{
$token = $this->get_val(substr($token, 1));
}
break;
}
}


return '<?php echo ' . implode(' ', $tokens) . '; ?>';
}

 

在大概530行处加入:

case 'math':
return $this->_compile_math_tag(substr($tag, 5));
break;


前台模板调用:
增加四则运算的ecshop模版类在模版中插入{math $a add $b}表示$a+$b用下面的字母替换上面的addsub   减法div   除法mak   乘法 

posted @ 2016-12-26 11:35  阿虎~  阅读(1732)  评论(0编辑  收藏  举报