本文實(shí)例講述了smarty自定義函數(shù)用法。分享給大家供大家參考,具體如下:
<?phprequire_once "smarty.config.php";//自定義一個(gè)函數(shù)//調(diào)用方法:<{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;}//自定義一個(gè)塊方式函數(shù)//調(diào)用方法<{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;}//定義一個(gè)計(jì)算方法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;}//注冊(cè)一下$smarty->register_function("liuyibao","test1");//注冊(cè)塊函數(shù)$smarty->register_block("liuyibao2","test2");//注冊(cè)函數(shù)jisuan$smarty->register_function("jisuan","jisuan");//替換變量$smarty->display("function.tpl");?>		














