<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="section.template.cache.handler.func.html" accesskey="P">Prev</a></td><td width="50%" align="center" valign="bottom">Chapter 15. Advanced Features 高级特性</td><td width="25%" align="right" valign="bottom"><a href="plugins.html" accesskey="N">Next</a></td></tr></table> # Resources[资源] **Table of Contents**[Templates from $template_dir](#) [$template_dir定义的模板][Templates from any directory](#) [从任何目录导入的模板][Templates from strings](#) [字符串定义的模板] [Template inheritance defined by PHP script](#) [php定义的模板继承][Templates from other sources](#) [从其它资源引入的模板][Default template handler function](#) [默认模板处理函数] The templates may come from a variety of sources. When you display() or fetch() a template, or when you include a template from within another template, you supply a resource type, followed by the appropriate path and template name. If a resource is not explicitly given, the value of $default_resource_type is assumed. 模板可以来自各种各样的资源:当你1、[display()](#)显示或者2、[fetch()](#)获取一个模板,或者,3、当你[{include}](#)包含的模板里面再包含另一个模板;这时你只要提供一个资源类型,填入适当的路径和模板名,就获取了模板资源。如果没有明确指出资源,那么,将取自[$default_resource_type](#)变量这个假定值。 ### Templates from $template_dir[$template_dir定义的模板] Templates from the $template_dir do not require a template resource, although you can use the file: resource for consistancy. Just supply the path to the template you want to use relative to the $template_dir root directory (no leading slash.) 来自[$template_dir](#)的模板不会请求模板资源,尽管你可以使用***file:资源***。只有在提供了相对于$template_dir目录的路径(没有前导斜杠,即file:...格式)给模板(才会请求模板资源)。 <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"> <a name="AEN31517" id="AEN31517"> </a><strong>Example 15.17. Using templates from the $template_dir</strong><br/><strong>例15-17. 使用$template_dir给出的模板 </strong> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><p>&lt;?php<br/> $smarty-&gt;display('index.tpl');<br/> $smarty-&gt;display('file:index.tpl'); // same as above 同上 <br/> ?&gt;</p> <p>From within a Smarty template</p> <p>{include file='index.tpl'}<br/> {include file='file:index.tpl'} {* same as above *}</p></td> </tr></table></div></td> </tr></table> ### Templates from any directory[从任何目录导入的模板] Templates outside of the $template_dir require the file: template resource type, followed by the absolute path to the template (with leading slash.) 在[$template_dir](#)范围外的模板请求***file:***模板资源类型,后接模板的绝对路径(带前导斜杠,即file:/...格式)。 <table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 提示 </caption> <tr><td>Note<br/> With security enabled, access to templates outside of the template_dir is not allowed.<br/> 如果开启安全,则不允许访问$template_dir外的模板。</td> </tr></table> <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"> <a name="AEN31518" id="AEN31518"> </a><span class="PROGRAMLISTING"><strong>Example 15.18. Using templates from any directory</strong></span><strong><br/> 例15-18. 使用来自任何目录的模板 </strong> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre>&lt;?php$smarty-&gt;display('file:/export/templates/index.tpl');$smarty-&gt;display('file:/path/to/my/templates/menu.tpl');?&gt; And from within a Smarty template:{include file='file:/usr/local/share/templates/navigation.tpl'}</pre></td> </tr></table></div></td> </tr></table> ### Windows Filepaths If you are using a Windows machine, filepaths usually include a drive letter (C:) at the beginning of the pathname. Be sure to use file: in the path to avoid namespace conflicts and get the desired results. 对于Windows系统,文件路径一般为路径名前包含一个盘符驱动号(如C:)。请确定在路径中使用了file:,以避免命名空间冲突。 <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"><a name="AEN31519" id="AEN31519"> </a><strong>Example 15.19. Using templates from windows file paths<br/> 例15-19.使用来自windows文件路径的模板</strong> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><p>&lt;?php<br/> $smarty-&gt;display('file:C:/export/templates/index.tpl');<br/> $smarty-&gt;display('file:F:/path/to/my/templates/menu.tpl');<br/> ?&gt;<br/><br/> And from within Smarty template:</p> <p>{include file='file:D:/usr/local/share/templates/navigation.tpl‘}</p></td> </tr></table></div></td> </tr></table> ### Templates from strings[字符串定义的模板] Smarty can render templates from a string by using the string: or eval: resource. The string: resource behaves much the same as a template file. The template source is compiled from a string and stores the compiled template code for later reuse. Each unique template string will create a new compiled template file. If your template strings are accessed frequently, this is a good choice. If you have frequently changing template strings (or strings with low reuse value), the eval:resource may be a better choice. The eval: resource evaluates the template source every time a page is rendered. This is a good choice for strings with low reuse value. If the same string is accessed frequently, the string: resource may be a better choice. Smarty可以渲染通过字符串传递进来的模板,使用方法:***string:资源***或***eval:资源***。 **String:资源**行为与模板文件很像。模板资源在字符串中得到编译,同时编译后的模板代码将被贮存起来以备后用。每个唯一的模板字符串将建立一个新的编译模板文件,如果访问模板字符串的频率很高,这会是个不错的主意。如果你经常更改模板字符串(或者字符串的重用值很低),***eval:资源***会是个更好的选择。 **eval:资源**用以在每次渲染页面时计算模板资源。当字符串的重用率不高时,使用本方法会是个不错的选择。而当频繁访问同一个字符串(模板)时,***string:资源***会是个更好的选择。 <table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 提示 </caption> <tr><td>Note<br/> With a string: resource type, each unique string generates a compiled file. Smarty cannot detect a string that has changed, and therefore will generate a new compiled file for each unique string. It is important to choose the correct resource so that you do not fill your disk space with wasted compiled strings.<br/> 使用<strong>string:资源</strong>类型,每个唯一的字符串生成一个编译文件。Smarty不能针测到字符串的改变,因此,(如果更改字符串)它又将为这个唯一的字符串生产一个新的编译文件。从这个特征来说,选择恰当的资源很重要,以避免因编译字符串的浪费使你的磁盘臃肿起来。</td> </tr></table> <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"> <a name="AEN31520" id="AEN31520"> </a><strong>Example 15.20. Using templates from strings</strong><br/><strong>例15-20. 使用来自字符串的模板 </strong> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><p>&lt;?php<br/> $smarty-&gt;assign('foo','value');<br/> $template_string = 'display {$foo} here';<br/> $smarty-&gt;display('string:'.$template_string); // compiles for later reuse 编译,以备后用<br/> $smarty-&gt;display('eval:'.$template_string); // compiles every time 每次编译<br/> ?&gt;</p> <p><br/> From within a Smarty template<br/> {include file="string:$template_string"} {* compiles for later reuse 编译起来给后面使用 *} <br/> {include file="eval:$template_string"} {* compiles every time 每次编译 *}</p></td> </tr></table></div></td> </tr></table> ### Template inheritance defined by PHP script[php定义的模板继承] The extends: resource is used to define child/parent relationships for template inheritance from the PHP script. For details see section of Template Interitance. ***extends:资源***用于由php脚本为模板继承定义的子/父关系。详细资料查看[模板继承](#)章节。 <table width="100%" border="0" cellpadding="0" cellspacing="0"><span class="EXAMPLE"> <tr><td><div><span class="EXAMPLE"> <a name="AEN31521" id="AEN31521"> </a><strong>Example 15.21. Using template inheritance from the PHP script</strong><br/><strong>例15-21. 使用来自php脚本的模板继承</strong> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><p>&lt;?php<br/> $smarty-&gt;display('extends:parent.tpl|child.tpl|grandchild.tpl'); <br/> ?&gt;</p> </td> </tr></table></span> <table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 提示 </caption> <tr><td>Note<br/> Use this when inheritance is required programatically. When inheriting within PHP, it is not obvious from the child template what inheritance took place. If you have a choice, it is normally more flexible and intuitive to handle inheritance chains from within the templates.<br/> 在需要继承时使用它(<em><strong>extends:资源</strong></em>)。php脚本定义的继承,并不能让人直观地感受到子模板的继承到底发生了什么。如果你可以选择,从模板内部处理继承链通常会更灵活和直观。</td> </tr></table></div></td> </tr></span></table> ### Templates from other sources[从其它资源引入的模板] You can retrieve templates using whatever possible source you can access with PHP: databases, sockets,files, etc. You do this by writing resource plugin functions and registering them with Smarty. See resource plugins section for more information on the functions you are supposed to provide. php程序里你可以使用所能访问的任何可能资源获取模板:数据库、socket接口、文件等等。方法为:写一个资源插件函数,然后通过Smarty[注册该函数](#)。 更多相关函数信息,请参阅[资源插件](#)部分。 <table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 提示 </caption> <tr><td><p>Note<br/> Note that you cannot override the built-in file: resource, but you can provide a resource that fetches templates from the file system in some other way by registering under another resource name.</p> <p>注意,你不能覆盖内置<em><strong>file:资源</strong></em>,但可以提供一个资源,通过将该资源注册在另一个资源名下的方式从文件系统中获取模板。<br/></p></td> </tr></table> <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"> <a name="AEN31522" id="AEN31522"> </a><strong>Example 15.22. Using custom resources </strong> <br/><strong>例15-22.使用自定义资源 </strong> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><p>&lt;?php<br/> // put these function somewhere in your application 将下面这些函数放在你程序的某个地方<br/> function db_get_template ($tpl_name, &amp;$tpl_source, $smarty_obj)<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/> }</p> <p>function db_get_timestamp($tpl_name, &amp;$tpl_timestamp, $smarty_obj)<br/> {<br/> // do database call here to populate $tpl_timestamp 在这里调用数据库赋值$tpl_timestamp <br/> // with unix epoch time value of last template modification. 最后的模板修改时间(unix纪元时间)<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 如果为真返回true,失败的话生成一个错误提示<br/> return true;<br/> }</p> <p>function db_get_secure($tpl_name, $smarty_obj)<br/> {<br/> // assume all templates are secure 假设所有模板安全<br/> return true;<br/> }</p> <p>function db_get_trusted($tpl_name, $smarty_obj)<br/> {<br/> // not used for templates 模板用不到的 <br/> }<br/> // register the resource name "db" 注册资源,命名“db”<br/> $smarty-&gt;register_resource("db", array("db_get_template",<br/> "db_get_timestamp",<br/> "db_get_secure",<br/> "db_get_trusted"));<br/> // using resource from php script 使用来自php脚本的资源<br/> $smarty-&gt;display("db:index.tpl");<br/> ?&gt;</p> <p>And from within Smarty template:<br/> {include file='db:/extras/navigation.tpl'} <br/></p></td> </tr></table><table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 译注 </caption> <tr><td>上例register_resource()应为registerResource()。</td> </tr></table></div></td> </tr></table> ### Default template handler function[默认模板处理函数] You can specify a function that is used to retrieve template contents in the event the template cannot be retrieved from its resource. One use of this is to create templates that do not exist on-the-fly. 在模板不能从其资源取出的情况下,你可以指定一个函数取回模板内容。用途之一就是在备用贮存器中建立一个之前不存在的模板。 <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"> <a name="AEN31523" id="AEN31523"> </a><span class="PROGRAMLISTING"><strong>Example 15.23. Using the default template handler function<br/> 例15-23.使用默认模板处理函数</strong></span> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING">&lt;?php// put this function somewhere in your application 将下面函数放在程序的某个地方function make_template ($resource_type, $resource_name, &amp;$template_source, &amp;$template_timestamp, &amp;$smarty_obj){ if( $resource_type == 'file' ) { if ( ! is_readable ( $resource_name )) { // create the template file, return contents. 建立模板文件,返回内容 $template_source = "This is a new template."; require_once SMARTY_CORE_DIR . 'core.write_file.php'; smarty_core_write_file( array( 'filename'=&gt;$smarty_obj-&gt;template_dir .$resource_name),$template_timestamp); return true; } } else { // not a file 文件不存在 return false; }}// set the default handler 设置默认处理$smarty-&gt;default_template_handler_func = 'make_template';?&gt;</pre></td> </tr></table><p> 参见<a href="advanced.features.streams.html">数据流</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="section.template.cache.handler.func.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.html" accesskey="N">Next</a></td></tr><tr><td width="33%" align="left" valign="top">Cache Handler Function<br/> 缓存处理函数</td><td width="34%" align="center" valign="top"><a href="advanced.features.html" accesskey="U">Up</a></td><td width="33%" align="right" valign="top">Extending Smarty With Plugins<br/> 以插件扩展Smarty</td></tr></table>