本文實例講述了smarty自定義函數用法。分享給大家供大家參考,具體如下:
<?phprequire_once "smarty.config.php";//自定義一個函數//調用方法:<{test1 times="4" size="5" con="Hello,Liuyibao!" color="red"}>function test1($args){$str="";for($i=0;$i<$args['times'];$i++){$str.="<p style='font-size:{$args['size']}em;color:{$args['color']}'>{$args['con']}</p>";}return $str;}//自定義一個塊方式函數//調用方法<{test1}><{/test1}>function test2($args,$con){$str="";for($i=0;$i<$args['times'];$i++){$str.="<p style='font-size:{$args['size']}em;color:{$args['color']}'>{$con}</p>";}return $str;}//定義一個計算方法function jisuan($args){switch($args['operate']){case "+" :$res=$args['num1']-$args['num2'];break;case "-" :$res=$args['num1']-$args['$num2'];break;case "*" :$res=$args['num1']*$args['$num2'];break;case "/" :$res=$args['num1']/$args['$num2'];break;}return $res;}//注冊一下$smarty->register_function("liuyibao","test1");//注冊塊函數$smarty->register_block("liuyibao2","test2");//注冊函數jisuan$smarty->register_function("jisuan","jisuan");//替換變量$smarty->display("function.tpl");?>