php—Smarty-4 (21),phpsmarty-421
Smarty函數---外掛程式
1) 內建函數 sysplugins 不允許修改
2) 自訂函數 plugins 可以修改
1、capture 捕獲一段內容,但不輸出
文法:
{capture name=‘var’}
內容
{/capture}
Name:將內容儲存到指定的變數中
{$smarty.capture.var}
在模板中,需要反覆輸出的大量內容,可以考慮使用capture捕獲。
2、config_load 載入設定檔
{config_load file=“file”section=“section”}
file:設定檔名稱
section:引用哪個配置節 選擇性參數
引用文法:
{#var#}
{$smarty.config.var}
3、foreach,foreachelse 用於遍曆數組
文法:
{foreach from=數組 key=鍵 name=名稱 item=內容 }
{foreachelse} 當要遍曆的數組沒有任何元素時,執行foreachelse後面的語句
{/foreach}
From:要遍曆的數組
Item:遍曆到的索引值
Key:遍曆到的鍵名
Name:為foreach語句命名
Foreach內建變數
$smarty.foreach.name.index :索引 從0開始
$smarty.foreach.name.iteration :索引 從1開始
$smarty.foreach.name.first :當第一次執行迴圈時,該值為true
$smarty.foreach.name.last :當最後一次執行迴圈時,該值為true
$smarty.foreach.name.total :迴圈總數
4、include 引用另一個模板檔案
{include file=“file” assign=“var” [var ...]}
File:要引入的模板檔案
Assign:將結果儲存,但本次不輸出
[var …] :選擇性參數,為模板檔案傳參
5、include_php 包含一個php檔案
{ include_php file=“file” assign=“var” once=“true|false”}
File:要引入的php檔案
Assign:儲存輸出結果,但本次不輸出
Once:是否只包含一次 true:是 false:否
這個函數在smarty2.6中是可用的,但在3.0中將這個函數移除,如果還想使用這個函數,可以引用SmartyBC.class.php這個類檔案
6、insert 在模板中,執行一個php函數
文法:
{ insert name=“func” [var ...] }
Name :被調用的php函數名
[var ...] :調用函數時指定參數 選擇性參數
Insert和include_php是同樣的。都是2.6中的函數,在3.0中,將這個功能放到SmartyBC.class.php這個檔案中了。
注意:inser引用的php函數的名稱必須以insert_為首碼
7、if elseif else
{if}
{elseif}
{else}
{/if}
8、ldelim 、rdelim 模板中的 { } 的轉義符
{ldelim}
{rdelim}
9、literal literal標記中間的內容不會被smarty所解析
{literal}
……
{/literal}
10、php 可以在一對php標記中寫php代碼
{php}
……
{/php}
http://www.bkjia.com/PHPjc/993264.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/993264.htmlTechArticlephp—Smarty-4 (21),phpsmarty-421 Smarty函數---外掛程式 1) 內建函數 sysplugins 不允許修改 2) 自訂函數 plugins 可以修改 1、capture 捕獲一段內容,...