<table summary="Header navigation table" width="100%" border="0" cellpadding="0" cellspacing="0"><tr><th colspan="3" align="center">Smarty - the compiling PHP template engine</th></tr><tr><td width="25%" align="left" valign="bottom"><a href="plugins.outputfilters.html" accesskey="P">Prev</a></td><td width="50%" align="center" valign="bottom">Chapter 16. Extending Smarty With Plugins</td><td width="25%" align="right" valign="bottom"><a href="plugins.inserts.html" accesskey="N">Next</a></td></tr></table>
# Resources[資源]
Resource plugins are meant as a generic way of providing template sources or PHP script components to Smarty. Some examples of resources: databases, LDAP, shared memory, sockets, and so on.
There are a total of four functions that need to be registered for each type of resource. Every function will receive the requested resource as the first parameter and the Smarty object as the last parameter. The rest of parameters depend on the function.
資源插件是一種必須提供模板資源或PHP腳本組件給Smarty的通用方式。部份屬于資源的例子:數據庫、LDAP、共享內存、sockets(套接字)等等。
每類資源須注冊4個函數。每一個函數接收請求資源作為第一個參數,Smarty對象作為最后一個參數。其它參數的設定取決于相應的函數。
> bool smarty_resource_name_source($rsrc_name, &$source, &$smarty);
string $rsrc_name;
string &$source;
object &$smarty;
bool smarty_resource_name_timestamp($rsrc_name, &$timestamp, &$smarty);
string $rsrc_name;
int &$timestamp;
object &$smarty;
bool smarty_resource_name_secure($rsrc_name, &$smarty);
string $rsrc_name;
object &$smarty;
bool smarty_resource_name_trusted($rsrc_name, &$smarty);
string $rsrc_name;
object &$smarty;
The first function, source() is supposed to retrieve the resource. Its second parameter $source is a variable passed by reference where the result should be stored. The function is supposed to return TRUE if it was able to successfully retrieve the resource and FALSE otherwise.
The second function, timestamp() is supposed to retrieve the last modification time of the requested resource, as a UNIX timestamp. The second parameter $timestamp is a variable passed by reference where the timestamp should be stored. The function is supposed to return TRUE if the timestamp could be succesfully determined, or FALSE otherwise.
The third function, secure()is supposed to return TRUE or FALSE, depending on whether the requested resource is secure or not. This function is used only for template resources but should still be defined.
The fourth function, trusted() is supposed to return TRUE or FALSE, depending on whether the requested resource is trusted or not. This function is used for only for PHP script components requested by {include_php} tag or {insert} tag with the src attribute. However, it should still be defined even for template resources.
第一個函數,source()用來檢索資源,第二個參數$source是一個對結果存放位置的引用傳遞。如果函數成功檢索到資源,將會返回true,否則返回false。
第二個函數,timestap()將會檢索請求資源的最后修改時間(UNIX時間戳)。第二個參數$timestap是一個對時間戳存放位置的引用傳遞。如果函數成功取得時間戳,將會返回true,否則返回false。
第三個函數,secure()將會返回true或false,這取決于被請求資源是否安全。本函數僅用于模板資源,但仍須定義。
第四個函數,trusted()將會返回true或false,這取決于被請求資源是否可信任。本函數僅用于[{include_php}](#)或[{insert}](#)標簽以*src*屬性請求的PHP腳本組件。但須定義,甚至對于模板資源也不例外。 譯注
| 資源插件的四個函數成套使用,即使用不到某項函數也須定義該函數。這有點像php迭代器。 |
|-----|
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"><a name="AEN41610" id="AEN41610"/> <b>Example 16.10. resource plugin<br/> 例16-10.資源插件</b> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><?php<br/> /*<br/>* Smarty plugin<br/>* -------------------------------------------------------------<br/>* File: resource.db.php<br/>* Type: resource<br/>* Name: db<br/>* Purpose: Fetches templates from a database<br/>* -------------------------------------------------------------<br/>*/<br/>function smarty_resource_db_source($tpl_name, &$tpl_source, $smarty)<br/>{<br/>// do database call here to fetch your template, 調用數據庫獲取模板<br/>// populating $tpl_source with actual template contents 用實際模板內容賦值$tpl_source <br/>$tpl_source = "This is the template text";<br/>// return true on success, false to generate failure notification 成功返回true,失敗則產生錯誤提示<br/>return true;<br/>}<br/>function smarty_resource_db_timestamp($tpl_name, &$tpl_timestamp, $smarty)<br/>{<br/>// do database call here to populate $tpl_timestamp 調用數據庫獲取模板修改的最后unix紀元值賦給$tpl_timestamp<br/>// with unix epoch time value of last template modification. <br/>// This is used to determine if recompile is necessary. 這里用來確定是否須重編譯<br/>$tpl_timestamp = time(); // this example will always recompile!<br/>// return true on success, false to generate failure notification<br/>return true;<br/>}<br/>function smarty_resource_db_secure($tpl_name, $smarty)<br/>{<br/>// assume all templates are secure 假定所有的模板安全<br/>return true;<br/>}<br/>function smarty_resource_db_trusted($tpl_name, $smarty)<br/>{<br/>// not used for templates 在這里未為模板設定 <br/>}<br/>?></td></tr></table><p> 參見<a href="api.register.resource.html">register_resource()</a>、<a href="api.unregister.resource.html">unregister_resource()</a>。</p></div></td></tr></table>
<table summary="Footer navigation table" width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td width="33%" align="left" valign="top"><a href="plugins.outputfilters.html" accesskey="P">Prev</a></td><td width="34%" align="center" valign="top"><a href="index.html" accesskey="H">Home</a></td><td width="33%" align="right" valign="top"><a href="plugins.inserts.html" accesskey="N">Next</a></td></tr><tr><td width="33%" align="left" valign="top">Output Filters[<br/> 輸出過濾器</td> <td width="34%" align="center" valign="top"><a href="plugins.html" accesskey="U">Up</a></td> <td width="33%" align="right" valign="top">Inserts<br/> 插入</td> </tr></table>
- Smarty模板編譯引擎
- 序
- 譯序
- I.開始
- 第一章. 什么是Smarty?
- 第二章. 安裝
- II.模板設計者篇
- 第三章.基本語法
- 注釋
- 變量
- 函數
- 屬性
- 雙引號里嵌入變量
- 數學運算
- 忽略Smarty解析
- 第四章.變量
- 從PHP分配的變量
- 從配置文件讀取的變量
- 變量范圍
- {$smarty}保留變量
- 第五章.變量調節器
- capitalize
- cat
- count_characters
- count_paragraphs
- count_sentences
- count_words
- date_format
- default
- escape
- indent
- lower
- nl2br
- regex_replace
- replace
- spacify
- string_format
- strip
- strip_tags
- truncate
- upper
- wordwrap
- 第六章.組合修改器
- 第七章.內置函數
- {$var=}
- {append}
- {assign}
- {block}
- {call}
- {capture}
- {config_load}
- {debug}
- {extends}
- {for}
- {foreach},{foreachelse}
- @index
- {function}
- {if},{elseif},{else}
- {include}
- {include_php}
- {insert}
- {ldelim},{rdelim}
- {literal}
- {nocache}
- {php}
- {section},{sectionelse}
- .index
- {while}
- 第八章.自定義函數
- {counter}
- {cycle}
- {eval}
- {fetch}
- {html_checkboxes}
- {html_image}
- {html_options}
- {html_radios}
- {html_select_date}
- {html_select_time}
- {html_table}
- {mailto}
- {math}
- {textformat}
- 第九章.配置文件
- 第十章.調試控制臺
- III.模板程序員篇
- 第十一章 常量
- SMARTY_DIR
- 第十二章 Smarty類變量
- $template_dir
- 第十三章.Smarty類方法
- append()
- appendByRef()
- assign()
- assignByRef()
- clearAllAssign()
- clearAllCache()
- clearAssign()
- clearCache()
- clearCompiledTpl()
- clearConfig()
- compileAllConfig()
- compileAllTemplates()
- configLoad()
- createData()
- createTemplate()
- disableSecurity()
- display()
- enableSecurity()
- fetch()
- getConfigVars()
- getRegisteredObject()
- getTags()
- getTemplateVars()
- isCached()
- loadFilter()
- registerFilter()
- registerPlugin()
- registerObject()
- registerResource()
- templateExists()
- unregisterFilter()
- unregisterPlugin()
- unregisterObject()
- unregisterResource()
- testInstall()
- 第十四章.緩存
- 建立緩存
- 多重緩存
- 緩存集合
- 控制插件輸出的可緩存性
- 第十五章.高級特性
- 安全
- 通過模板更改設置
- 模板繼承
- 數據流
- 對象
- 靜態類
- 預過濾器
- 后過濾器
- 輸出過濾器
- 緩存處理函數
- 資源
- 第十六章.以插件擴展Smarty
- 插件如何工作
- 命名約定
- 編寫插件
- 模板函數
- 調節器
- 塊函數
- 編譯函數
- 預濾器/后濾器
- 輸出過濾器
- 資源
- 插入
- Ⅳ.附錄
- 第十七章.疑難解答
- Smarty/PHP 錯誤
- 第十八章.使用技巧和經驗
- 空白變量處理
- 默認變量處理
- 傳遞變量標題給頭模板
- 日期
- WAP/WML
- 組件化模板
- 拒絕電子郵件地址
- 第十九章. 相關資源
- 第二十章. 漏洞
- 3.0安裝包
- 2.x版本升級至3.x版本的提示
- 3.0.x使用指南
- 翻譯人員列表