《PHP學習:smarty模板引擎中自定義函數的方法》要點:
本文介紹了PHP學習:smarty模板引擎中自定義函數的方法,希望對您有用。如果有疑問,可以聯系我們。
PHP學習本文實例講述了smarty 自定義函數辦法,分享給大家供大家參考.具體如下:
PHP學習本實例目的:輸出 times 次 con的內容(輸出4次hello world)
PHP學習文件1:
代碼如下:
<?php
//創建smarty對象
require_once("./libs/Smarty.class.php");
$smarty = new Smarty();
//自定義一個函數
//說明:(1)、$arr為一個數組;(2)、tpl調用形式{test times="4" size="5" con="hello,world" color="red"}
function test($arr){
?$str = "";
?for($i=0;$i<$arr['times'];$i++){
? $str .= "<font size='".$arr['size']."' color='".$arr['color']."'>".$arr['con']."</font>";
?}
?return $str;
}
//注冊函數 registerPlugin
$smarty->registerPlugin("function","test","test");//第二個參數是模板文件調用的函數名稱,可變;第三個參數是上面自定義的函數名稱;相應于一個對應關系
PHP學習$smarty->display("temp.tpl");
?>
PHP學習模板文件:temp.tpl
代碼如下:
<html>
<h2>smarty自定義函數的使用</h2>
{test times="3" con="hello world" size="3" color="green"}
</html>
注意:smarty 3.1.8 已經不支持注冊函數 register_function,應換成 registerPlugin
PHP學習希望本文所述對大家的php程序設計有所贊助.
歡迎參與《PHP學習:smarty模板引擎中自定義函數的方法》討論,分享您的想法,維易PHP學院為您提供專業教程。
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/12567.html