Smarty:
1、 自定義函數(自定義標籤)
1) php文件function定義函數
註冊步驟(register_function()、register_block())
Tpl文件類似於html標籤
<{title num=”10” }>
成對出現---塊
<{tilte}>……..<{/title}>
2)當作插件
./plugins
*.function.*.php
Function smarty_function.
*.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” item=
變量:$smarty.foreach.name1.first last total etc.
Section---for 索引數組
$data[]=$row;
Name=”out”
$data[out].ip
…..
Start step max
PHP
Cache:提高效率
*.php *.tpl
smarty--- 合併後編譯 (php與html混合)
templates_c
*.php
優點:省略了重新合併、編譯的時間;
連接數據庫不能省略
增加緩存:./cache/*.html
減少了訪問數據庫的次數
Display(“”,””)
$_GET[“”]
$_SERVER[“REQUST_URI”]
局部緩存
實時性要求比較高
例如:登陸用戶名稱
歡迎Amdin登陸到****
例如:時間日期
解決步驟: 自定義函數(自定義插件)
第一種:插件形式
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定義同上