Smarty中用戶自定義插件
用戶自定義插件:
{ts:變量|函數[:參數]}:修飾函數插件(modify)
{ts:插件名 屬性=值 屬性=值...} 函數類型插件(function)
{ts:插件名 屬性=值 屬性=值...} block類型插件
{ts:/插件名} #插件閉合的結尾一定不可以漏#
Libs目錄詳解:
Plugins: 用戶自定義插件目錄
Sysplugins:系統插件目錄
修飾函數插件:
libs/plugins/創建固定格式文件:
Modifier.插件名.php
編寫規定規範的函數:
最後結果數據使用return返回。
function smarty_modifier_插件名($string,參數1,參數2...){}
$string:指的是被修飾的變量
參數1:參數1 作為函數調用的第一個參數,也可以只有一個參數
插件使用:
{ts:變量名|插件名:參數1,參數2....}
=>smarty_modifier_插件名(變量名,參數1,參數2....)
【modifier.strcon.php】 #文件必須放在/libs/plugins/下#
<?php function smarty_modifier_strcon($string,$string2){ $s = $string.$string2; return $s; } ?>
block類型的插件:
libs/plugins/創建文件
Block.插件名.php
block插件函數定義:
function smarty_block_插件名($params, $content, $template){
if (is_null($content)) return;
Return 返回內容
}
$params:以屬性當鍵名,以屬性值作為數組元素組成的一維關聯數組。
$content: 被修飾的內容
$template:smarty對象
調用插件:
{ts:插件名 屬性=值 屬性=值...}
{ts:/插件名}
【block.hotlink.php】 #文件必須放在/libs/plugins/下#
<?php // 熱點關鍵詞 php=>http://www.php.com hotlink插件 function smarty_block_hotlink($params,$content,$template){ foreach($params as $key=>$vo){ $link = "<a href=‘{$vo}‘>".strtoupper($key)."</a>"; $content = str_replace($key,$link,$content); } return $content; } ?>
【block.php】
<?php require_once("./smarty.inc.php"); $tpl -> display("block.html"); ?>
【block.html】
{ts:hotlink asp="http://www.baidu.com" php="http://www.php.com" Shuo_128="http://www.cnblogs.com/shuo-128/"} 大家好,我是Shuo_128,點這裏跳轉到asp,點這裏跳轉到php {ts:/hotlink} <hr/> {ts:/hotlink}
#在block.php中獲取前臺block.html中的值#
function類型插件:
用於網站js效果,數據展示,內容展示等
{ts:插件名 屬性=值 屬性=值...} 函數類型插件(function)
libs/plugins/文件定義
Function.插件名.php
函數定義格式:
Function smarty_function_插件名($params,$template){
$params: 屬性名當鍵名,屬性值當數組元素值一維關聯
$template:smarty對象
Return 數據:如果沒有數據遍歷就直接進行數據輸出
Assign: 如果有數據遍歷那就需要使用assign向模板傳遞數據。
}
調用: {ts:插件名 屬性=值 屬性=值...}
{ts:sqllist table="" max="" orderby="" sql=""}
【sqllist.php】
<?php require_once("./smarty.inc.php"); $tpl -> display("sqllist.html"); ?>
【sqllist.php】
{ts:sqllist table="ts_news" max="1"} <hr/> {ts:sqllist sql="select * from ts_news" max=2}
#在sqllist.php中獲取前臺sqllist.html中的值#
Smarty中用戶自定義插件