<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                <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>
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看