生活随笔
收集整理的這篇文章主要介紹了
smarty模板引擎_7-自定义函数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
擴展自定義函數的兩種方式
1、通過registerPlugin()注冊,僅對當前模板有效
[php]?view plaincopy
function?getVerify($params,$smarty){?? ????if(empty($params['type'])||$params['type']>3||$params['type']<1){?? ????????$type=1;?? ????}else{?? ????????$type=$params['type'];?? ????}?? ????if(empty($params['length'])||$params['length']>6||$params['length']<=0){?? ????????$length=4;?? ????}else{?? ????????$length=$params['length'];?? ????}?? ????if($type==1){?? ?????????? ????????$string=join('',range(0,9));?? ????}elseif($type==2){?? ????????$string=join('',array_merge(range('a','z'),range('A','Z')));?? ????}elseif($type==3){?? ????????$string=join('',array_merge(range(0,9),range('a','z'),range('A','Z')));?? ????}?? ????return?substr(str_shuffle($string),0,$length);?? }??
規定函數里需要用到的參數都要用$params這個數組傳入
$smarty,Smarty的對象
[php]?view plaincopy
$smarty->registerPlugin('function','verifyCode','getVerify');??
function表示是自定義函數
verifyCode是自定義函數的名稱
getVerify自定義函數中的調用的函數
調用該自定義函數
[php]?view plaincopy
<{verifyCode}>?? <hr?/>?? <{verifyCode?type='2'}>?? <hr?/>?? <{verifyCode?type='3'?length='5'}>??
2、以插件形式擴展全局自定義函數
[php]?view plaincopy
function?smarty_function_verify($params,$smarty){?? ????if(empty($params['type'])||$params['type']>3||$params['type']<1){?? ????????$type=1;?? ????}else{?? ????????$type=$params['type'];?? ????}?? ????if(empty($params['length'])||$params['length']>6||$params['length']<=0){?? ????????$length=4;?? ????}else{?? ????????$length=$params['length'];?? ????}?? ????if($type==1){?? ?????????? ????????$string=join('',range(0,9));?? ????}elseif($type==2){?? ????????$string=join('',array_merge(range('a','z'),range('A','Z')));?? ????}elseif($type==3){?? ????????$string=join('',array_merge(range(0,9),range('a','z'),range('A','Z')));?? ????}?? ????return?substr(str_shuffle($string),0,$length);?? }??
函數命名規則:smarty_function_
verify
($params,$smarty){}
插件命名規則:function.verify.PHP
插件目錄:plugins文件夾
調用該插件
[php]?view plaincopy
<{verify}>?? <hr?/>?? <{verify?type='2'}>?? <hr?/>?? <{verify?type='3'?length='5'}>??
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的smarty模板引擎_7-自定义函数的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。