在ecshop模板使用自定義函數


https://blog.csdn.net/shaolinld/article/details/46400485

 

在ecshop模板使用自定義函數

可以增加自定義函數,在模板直接調用,例如: {$userId|get_user_name} 或{$imgUrl|set_width_height:100:200}

 

案例一(處理圖片):

 

1.模板里使用{$imgurl|getpic:100:200}

 

2.includes/lib_base.php里面增加函數

function getpic($str, $width, $height){
 $len= stripos($str, '.');

 if($len !== false){
  $str= substr($str, 0, $len).'_'.$width.'-'.$height.stristr($str, '.');
   return $str;
 }
 else{
  return '/images/blank.gif';
 }
}

 

3.includes/cls_template.php里624行

case 'strip_tags':
 $p = 'strip_tags(' . $p . ')';
 break;
 后面增加處理函數

 case 'getpic':
 $p = 'getpic(' . $p . ",$s[1],$s[2])";
 break;

 

 

案例二(根據地區ID返回名稱):

1.模板里使用{$provinceId|get_region_name}

 

2.includes/lib_base.php里面增加函數

function get_region_name($id){
        $id = intval($id);
        $sql = "SELECT region_name FROM " . $GLOBALS['ecs']->table('region') . " WHERE region_id = $id";
        $list = $GLOBALS['db']->getRow($sql, true);
        return $list['region_name'];
}

 

3.includes/cls_template.php里624行

case 'strip_tags':
        $p = 'strip_tags(' . $p . ')';
        break;

 //后面增加處理函數:開始
case 'get_region_name':
        $p = 'get_region_name(' . $p . ")";
        break;
//后面增加處理函數:結束

 default:
        # code...
        break;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM