<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>&lt;?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, &amp;$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, &amp;$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/>?&gt;</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>