smarty自定義函數(shù)用法示例
來源:易賢網(wǎng) 閱讀:1224 次 日期:2016-08-19 15:24:46
溫馨提示:易賢網(wǎng)小編為您整理了“smarty自定義函數(shù)用法示例”,方便廣大網(wǎng)友查閱!

本文實例講述了smarty自定義函數(shù)用法。分享給大家供大家參考,具體如下:

<?php

require_once "smarty.config.php";

//自定義一個函數(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;

}

//自定義一個塊方式函數(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;

}

//定義一個計算方法

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");

//注冊塊函數(shù)

$smarty->register_block("liuyibao2","test2");

//注冊函數(shù)jisuan

$smarty->register_function("jisuan","jisuan");

//替換變量

$smarty->display("function.tpl");

?>

希望本文所述對大家基于smarty模板的PHP程序設計有所幫助。

更多信息請查看網(wǎng)絡編程
易賢網(wǎng)手機網(wǎng)站地址:smarty自定義函數(shù)用法示例
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點

版權(quán)所有:易賢網(wǎng)