1、自定義函數
1)PHP檔案function定義函數
中間需要注冊(register_function()、register_block())
Tpl檔案類似于html标簽,一般情況下會寫成如下格式<{title num=”10”}>
自定義函數的主要目的是增強功能也被稱做自定義标簽
也可成對出現――稱“塊”
在寫法上要注意 <{title}>…..<{/title}>
版本較高的并不支援
2)當作插件寫入
./plugins
如果針對的是單标記 *.block.*.php
打開該檔案 function smarty_block.
擴充smarty标簽功能子產品,自己進行編輯
2、内建函數
Include 包含子模闆 調用模闆可分成三部分 單标記
子模闆中也可以使用目前頁聲明的smarty變量
Include(“head.tpl”);
Config_load 界面的調整 單标記
<config_load file=”” section=”one”>
Capture 成對的标記 <{capture}>……<{/capture}>
$smarty.capture.
If …elseif…else /if: elseif中間不能出現空格
條件表達式不能加小括号()
= = > <
Eq gt lt ge le
Foreach ――foreach 關聯數組 索引資料
From=”要循環的數組名” name=”name1”
變量:$smarty.foreach.name1.first last total etc.
Item=
Section - -for 索引數組
把關聯數組重新進行儲存相當于存成一個二維數組
$data[]=$row; 需要注意給本次循環指定名稱name=
Name=”out” $data[out]
Cache:提高效率
*.php *.tpl
Smarty-----合并後編譯(php與html混合的檔案)
Templates_c
*.*
優點:省略了重新合并、編譯的時間;
連接配接資料庫不能省略
增加緩存:./cache/*.html
減少了通路資料庫的次數
Display(“”,””)
$_GET[]
$_SERVER[“REQUST_URI”]
局部緩存
實時更新性要求比較高的例如登陸使用者名稱
歡迎amdin登陸到XXXXX
時間日期
解決步驟:自定義函數(自定義插件)
1) 指定該函數名稱nocache
2) 建立檔案./plugins/block.nocache.php
内容:
<?php
function smarty_block_nocache($args, $content){
return $content;}
?>
3)*.php
….
$tpl->assign(“date,date(“H:i:s”));
….
*.tpl
<{nocache}><{$date}><{/nocache}>
所有插件預設被緩存
Smarty. 712 行
Else{
if($tag_command==nocache){
$this->_plugins['block'][$tag_command]= array($plugin_func, null, null, null, false);}
Else{
$this->_plugins['block'][$tag_command]= array($plugin_func, null, null, null, true);
}
}
第二種:php檔案内自定義函數并注冊
*.php
$tpl->register_block(“nocache”,”fun1”,false);
function fun1($args,$content){return$content;}
$tpl->assign(“date”,date(“H:i:s”));
*.tpl
<{nocache}><{$date}><{/nocache}>
第三種:smarty内建函數insert
定義一個inser标簽要使用的處理函數
函數名格式為:
insert_xx(array $params, object &$smarty)
其中的xx是insert的name,也就是說,如果你定義的函數為insert_abc,則模闆中使用方法為{insert name=abc}參數通過
$params傳入
也可以做成insert插件,檔案名命名為:insert.xx.php,函數命名為:smarty_insert_aa($params,&$smarty),xx定義同上