欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

smarty模板引擎中自定義函數(shù)的方法

 更新時(shí)間:2015年01月22日 15:36:14   投稿:shichen2014  
這篇文章主要介紹了smarty模板引擎中自定義函數(shù)的方法,實(shí)例分析了自定義函數(shù)的定義、注冊及調(diào)用技巧,需要的朋友可以參考下

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

本實(shí)例目的:輸出 times 次 con的內(nèi)容(輸出4次hello world)

文件1:

復(fù)制代碼 代碼如下:
<?php
//創(chuàng)建smarty對象
require_once("./libs/Smarty.class.php");
$smarty = new Smarty();
//自定義一個(gè)函數(shù)
//說明:(1)、$arr為一個(gè)數(shù)組;(2)、tpl調(diào)用形式{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;
}
//注冊函數(shù) registerPlugin
$smarty->registerPlugin("function","test","test");//第二個(gè)參數(shù)是模板文件調(diào)用的函數(shù)名稱,可變;第三個(gè)參數(shù)是上面自定義的函數(shù)名稱;相應(yīng)于一個(gè)對應(yīng)關(guān)系

$smarty->display("temp.tpl");
?>

模板文件:temp.tpl

復(fù)制代碼 代碼如下:
<html>
<h2>smarty自定義函數(shù)的使用</h2>
{test times="3" con="hello world" size="3" color="green"}
</html>

注意:smarty 3.1.8 已經(jīng)不支持注冊函數(shù) register_function,應(yīng)換成 registerPlugin

希望本文所述對大家的php程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論