当前位置:首页 > CMS教程 > ecshop > 列表

ecshop模板支持php数据运算的代码实例

发布:smiling 来源: PHP粉丝网  添加日期:2015-12-07 12:59:48 浏览: 评论:0 

我的echsop中需要在模板中用到运算功能,所以就对ecshop进行了二次开发,现在把支持echsop模板php运算的代码分享出来.

将运算代码集成到ecshop的步骤如下.

在 cls_template.php 底部加入函数:

  1. /** 
  2. * 处理math中的公式. 
  3.  
  4. * */ 
  5.       
  6. function get_math_para($val){ 
  7.  
  8. $pa= $this->str_trim($val); 
  9.  
  10. foreach($pa AS $value) 
  11.  
  12. { 
  13.  
  14. if(strrpos($value, '=')) 
  15.  
  16. { 
  17.  
  18. list($a, $b) = explode('=', str_replace(array(' ', '"', "'", '"'), '', $value)); 
  19.  
  20. if(strpos($b, '$') >= 0) 
  21.  
  22. { 
  23.  
  24. //$b为类似的1+2,$abc*123等 
  25.  
  26. $pattern= "/\\$[_a-zA-z]+[a-zA-Z0-9_]*/"; 
  27.  
  28. preg_match($pattern, $b,$arr); 
  29.  
  30. if($arr) { 
  31.  
  32. foreach($arr as $match) { 
  33.  
  34. $v= $this->get_val(substr($match, 1)); 
  35.  
  36. $b= str_replace($match, $v, $b); 
  37. //phpfensi.com 
  38. } 
  39.  
  40. } 
  41.  
  42. } 
  43.  
  44. } 
  45.  
  46. } 
  47.  
  48. return$b; 
  49.  
  50. } 

在大概530行处加入:

  1. case 'math': 
  2.  
  3. $t= $this->get_math_para(substr($tag, 8)); 
  4.  
  5. return'<?php echo '. $t. '; ?>'; 
  6.  
  7. break; 

前台模板调用:{math equation=”$key+1″}这里注意用双引号.

Tags: ecshop模板 php数据运算

分享到: