💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
<table width="100%" summary="Header navigation table" border="0" cellpadding="0" cellspacing="0"><tr><th colspan="3" align="middle">Smarty - the compiling PHP template engine</th></tr><tr><td width="20%" align="left" valign="bottom"><a accesskey="P" href="smarty.constants.html">Prev</a></td><td width="80%" align="middle" valign="bottom"/><td width="10%" align="right" valign="bottom"><a accesskey="N" href="api.functions.html">Next</a></td></tr></table> # Chapter 12. Smarty Class Variables 第十二章.Smarty类变量 **Table of Contents [内容列表]**[$template_dir](#) [模板目录变量] [$compile_dir](#) [编译目录变量] [$config_dir](#) [配置目录变量] [$plugins_dir](#) [插件目录变量] [$debugging](#) [调试变量] [$debug_tpl ](#)[调试模板变量] [$debugging_ctrl](#) [调试控制变量] [$auto_literal](#) [自动忽略解析] [$autoload_filters](#) [自动加载过滤器变量] [$compile_check](#) [编译检查变量] [$force_compile](#) [强迫编译变量] [$caching](#) [缓存变量] [$cache_dir](#) [缓存目录变量] [$cache_lifetime](#) [缓存生存时间变量] [$cache_handler_func](#) [缓存处理函数变量] [$cache_modified_check](#) [缓存修正检查变量] [$config_overwrite](#) [配置.覆盖变量] [$config_booleanize](#) [配置.布尔型变量] [$config_read_hidden](#) [配置.读取隐藏变量] [$config_fix_newlines](#) [配置.固定换行符变量] [$default_template_handler_func](#) [默认模板处理函数变量] [$php_handling](#) [PHP处理变量] [$trusted_dir](#) [信任目录变量] [$left_delimiter](#) [左定界符变量] [$right_delimiter](#) [右定界符变量] [$compiler_class](#) [编译类变量] [$request_vars_order](#)[](#)[变量顺序变量] [$request_use_auto_globals](#) [自动全局变量] [$compile_id](#) [编译ID变量] [$use_sub_dirs](#) [子目录变量] [$default_modifiers](#) [默认调节器变量] [$default_resource_type](#) [默认源类型变量] These are all of the available Smarty class variables. You can access them directly, or use the corresponding setter/getter methods. 本章列举了所有有效的Smarty类变量,你可以直接访问它们,或者使用对应的setter/getter方法。 <table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 提示 </caption> <tr><td>Note<br/> All class variables have magic setter/getter methods available. setter/getter methods are camelCaseFormat, unlike the variable itself. So for example, you can set and get the $smarty-&gt;template_dir variable with $smarty-&gt;setTemplateDir($dir) and $dir = $smarty-&gt;getTemplateDir() respectively.<br/> 所有类变量都有自己的魔术setter/getter方法,setter/getter方法为驼峰命名格式(译注:类方法见下一章),这与变量本身不同(译注:类变量正如你所见到的,为单词间加下划线'_')。因此,举例说,你可以使用$smarty-&gt;setTemplateDir($dir)和$dir = $smarty-&gt;getTemplateDir()分别设置和读取$smarty-&gt;template_dir变量。</td> </tr></table> <table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 提示 </caption> <tr><td>Note<br/> See Changing settings by template section for how to change Smarty class variables for individual templates.<br/> 关于怎样设置个别Smarty类变量部份参考<a href="advanced.features.changing.settings.by.tem.html">通过模板更改设置</a>章节。</td> </tr></table> # $template_dir[模板目录变量] This is the name of the default template directory. If you do not supply a resource type when including files, they will be found here. By default this is "./templates", meaning that it will look for the templates directory in the same directory as the executing php script. 该变量定义默认模板目录的名字。当包含文件时,如果未提供一个源类型(即源地址),那么将会到模板目录中寻找。默认情况下,目录是:“./templates”,也就是说他将会在和php执行脚本相同的目录下寻找模板目录。 <table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 技术提示 </caption> <tr><td>Technical Note: It is not recommended to put this directory under the web server document root.<br/> 不推荐把模板目录放在web服务器根目录下。</td> </tr></table> **译者例:模板文件夹移位时,模板目录变量路径的问题** <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><p>Smarty不推荐将模板文件、配置文件放置在服务器目录下,以Smarty自带的demo文件夹为示例,这时我们可以在demo下新建一个文件夹存放php脚本文件和编译文件,比如这个新文件夹命名为‘abc’,以文件夹abc为服务器根目录,这样就避开了问题。这时模板目录变量的路径写法则应改变,不然Smarty找不到模板文件。</p> <p>include_once('../../libs/Smarty.class.php'); //这时以文件夹abc为相对路径为基点<br/> $smarty = new Smarty;<br/> $smarty-&gt;assign('abc','hello!');<br/> $tpl= $smarty-&gt;createTemplate('../templates/test.html',$smarty); //这里写基于abc文件夹位置的模板文件相对路径<br/> $tpl-&gt;display();<br/> 这时你可以看到在abc下自建了一个编译目录。<br/><br/> 当然也可以设置$template_dir的绝对路径:<br/> include_once('../../libs/Smarty.class.php');<br/> $smarty = new Smarty;<br/> $tem_dir = $smarty-&gt;template_dir = 'c:/xx/xxx/smarty/demo/templates/';<br/> $smarty-&gt;assign('abc','hello!');<br/> $tpl= $smarty-&gt;createTemplate($tem_dir.'test.html',$smarty);<br/> $tpl-&gt;display();<br/></p></td> </tr></table> # $compile_dir[编译目录变量] This is the name of the directory where compiled templates are located. By default this is ./templates_c, meaning that Smarty will look for the templates_c/ directory in the same directory as the executing php script. This directory must be writeable by the web server, see install for more info. 本变量定位编译模板的目录名字。默认情况下,目录是:“./templates_c”,也就是说他将会在和php执行脚本相同的目录下寻找“./templates_c”编译目录。该目录在服务器中必须可写,参考[安装](#)一节。 <table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 技术提示 </caption> <tr><td><b>Technical Note: </b> This setting must be either a relative or absolute path. include_path is not used for writing files.<br/> 该设置必须是一个相对或绝对路径。包含路径不可用于写文件。 </td> </tr></table> <table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 技术提示 </caption> <tr><td><b>Technical Note: </b> It is not recommended to put this directory under the web server document root.<br/> 不推荐把编译目录放在web服务器根目录下。</td> </tr></table> # $config_dir[配置目录变量] This is the directory used to store config files used in the templates. Default is "./configs", meaning that it will look for the configs directory in the same directory as the executing php script. 本变量用于设置存放模板配置文件的目录,默认情况下,目录是:“./configs”,这意味着它将会在和php执行脚本相同的目录下搜索配置目录。 <table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 技术提示 </caption> <tr><td>Technical Note: It is not recommended to put this directory under the web server document root. <br/> 不推荐把编译目录放在web服务器根目录下。 </td> </tr></table> # $plugins_dir[插件目录变量] This is the directory or directories where Smarty will look for the plugins that it needs. Default is plugins/ under the SMARTY_DIR. If you supply a relative path, Smarty will first look under the SMARTY_DIR, then relative to the current working directory, then relative to the PHP include_path. If $plugins_dir is an array of directories, Smarty will search for your plugin in each plugin directory in the order they are given. 本变量设置Smarty寻找所需插件的目录。默认是在SMARTY_DIR(即Smarty类所在目录)目录下的“plugins”目录。如果提供了一个相对路径,Smarty将首先在SMARTY_DIR目录下寻找,然后到当前工作目录下寻找,继而到php包含路径中的每个路径中寻找。如果$plugins_dir是个目录数组,那么Smarty将根据给定的命令在目录数组中逐个搜索所需插件。 <table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 技术提示 </caption> <tr><td><p>Technical Note<br/> For best performance, do not setup your $plugins_dir to have to use the PHP include path.Use an absolute pathname, or a path relative to SMARTY_DIR or the current working directory.</p> <p>为了获取最佳性能,不要将$plugins_dir设置为必须使用php包含路径(包含include()、include_once()、request()、request_once()等函数)的地步。可以设置为绝对路径、SMARTY_DIR的相对路径或当前工作路径目录。</p></td> </tr></table> **Example 12.1. Appending a local plugin dir 例 12-1.设置一个本地插件路径** <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td>&lt;?php<br/> $smarty-&gt;plugins_dir[] = 'includes/my_smarty_plugins';<br/> ?&gt;</td> </tr></table> **Example 12.2. Multiple $plugins_dir 例 12-1.多维$plugins_dir** <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td>&lt;?php<br/> $smarty-&gt;plugins_dir = array(<br/>'plugins', // 在SMARTY_DIR目录下<br/>'/path/to/shared/plugins',<br/>'../../includes/my/plugins'<br/>);<br/>?&gt;</td> </tr></table> # $debugging[调试变量] This enables the debugging console. The console is a javascript popup window that informs you of the included templates, variables assigned from php and config file variables for the current script. It does not show variables assigned within a template with the {assign} function. The console can also be enabled from the url with $debugging_ctrl. See also {debug}, $debug_tpl, and $debugging_ctrl. 本变量用于启动调试控制台。该控制台是一个javascript窗口,向你报告加载模板的脚本中所有php和配置文件变量,但它不会显示由[{assign}](#)函数加载的内嵌模板分配的变量。 在url中使用[$debugging_ctrl](#)值同样可以启动调试控制台。 参见[{debug}](#)、[$debug_tpl](#)、和[$debugging_ctrl](#)。 # $debug_tpl[调试模板] This is the name of the template file used for the debugging console. By default, it is named debug.tpl and is located in the SMARTY_DIR. See also $debugging and the debugging console section. 本变量设置调试控制台的模板文件名字。默认情况下,其名字为:debug.tpl,位于Smarty目录中。 参见[$debugging](#)和[调试控制台](#)一节。 # $debugging_ctrl[调试控制变量] This allows alternate ways to enable debugging. NONE means no alternate methods are allowed. URL means when the keyword SMARTY_DEBUG is found in the QUERY_STRING, debugging is enabled for that invocation of the script. If $debugging is TRUE, this value is ignored. 本变量允许以交替的方式启动调试控制。NONE表示不允许交替方式。URL表示当关键词'SMARTY_DEBUG'出现在QUERY_STRING(查询字符串)中时,此脚本将调用调试控制。如果[$debugging](#)变量设为true,则忽略本调试控制变量。 **Example 12.3. $debugging_ctrl on localhost 例 12-3.设置本地的$debugging_ctrl变量** <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td>&lt;?php<br/> // shows debug console only on localhost ie<br/> // http://localhost/script.php?foo=bar&amp;SMARTY_DEBUG<br/> $smarty-&gt;debugging = false; // the default<br/> $smarty-&gt;debugging_ctrl = ($_SERVER['SERVER_NAME'] == 'localhost') ? 'URL' : 'NONE');<br/> ?&gt;</td> </tr></table> 参见[调试控制](#)一节和[$debugging](#)变量。 # $auto_literal[自动忽略解析] The Smarty delimiter tags { and } will be ignored so long as they are surrounded by white space. This behavior can be disabled by setting auto_literal to false. 只要Smarty定界符标签‘{’和‘}’内侧包含空格,那么模板将忽略定界符里面内容的解析。可以将$auto_literal变量设置为false取消上述规则。 <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td>&lt;?php<br/>$smarty-&gt;auto_literal = false;<br/>?&gt;</td> </tr></table> 参见[忽略Smarty解析](#)一节。 译注 | 不知将auto_literal翻译成什么好,反正就是那样的意思啦~~ | |-----| # $autoload_filters[自动加载过滤器变量] If there are some filters that you wish to load on every template invocation, you can specify them using this variable and Smarty will automatically load them for you. The variable is an associative array where keys are filter types and values are arrays of the filter names. For example: 如果你希望在每次模板调用过程中加载过滤器,你可以使用此变量指定过滤器,Smarty将自动为你加载它们。该变量是一个关联数组,键是过滤器类型,值是过滤器名字所组成的数组。例如: | ~~~ <?php $smarty->autoload_filters = array('pre' => array('trim', 'stamp'),'output' => array('convert')); ?> ~~~ | |-----| 参见[registerFilter()](#)和[loadFilter()](#)函数。 # $compile_check[编译检查变量] Upon each invocation of the PHP application, Smarty tests to see if the current template has changed (different time stamp) since the last time it was compiled. If it has changed, it recompiles that template.If the template has not been compiled, it will compile regardless of this setting. By default this variableis set to TRUE. Once an application is put into production (ie the templates won't be changing), the compile check step is no longer needed. Be sure to set $compile_check to FALSE for maximal performance. Note that if you change this to FALSE and a template file is changed, you will *not* see the change since the template will not get recompiled. If $caching is enabled and $compile_check is enabled, then the cache files will get regenerated if an involved template file or config file was updated. See $force_compile and clear_compiled_tpl() . 每次调用PHP应用程序,Smarty会试着查看自上次编译时间以来,当前模板是否被修改过。如果修改过了,她会重新编译那个模板。如果模板还没有被编译过,她将编译模板而不管编译检查如何设置。默认情况下本编译检查变量设置为true。 一旦一个应用程序投入产品中(假如模板将不再修改),就不再需要编译检查这一步了。为了获得最大性能,确定将$compile_check设为"false"。注意:如果设为了"false",虽然模板文件被修改,但你不会看到修改结果,因为模板没有得到重新编译。如果启动了缓存([$caching](#))和编译检查($compile_check),一旦有关模板文件或配置文件被更新,缓存文件将会被重建。 参见[$force_compile](#)和[clear_compiled_tpl()](#)。 # $force_compile[强迫编译变量] This forces Smarty to (re)compile templates on every invocation. This setting overrides $compile_check. By default this is FALSE. This is handy for development and debugging. It should never be used in a production environment. If $caching is enabled, the cache file(s) will be regenerated every time. 每次调用模板时强制Smarty(重新)编译模板,这项设置会覆盖[$compile_check](#)变量,使后者的设置失效,默认情况下,本变量为false(假),即Smarty并不强制每次重新编译模板。它对于开发和调试很方便,但它决不能使用于产品环境下。如果启动了缓存,每次将会重新生成缓存文件。 译注 | 本变量设为true的情况一般为开发和调试;因为smarty将模板编译为php脚本时开销很大,比纯php运行时速度慢10倍以上,甚至是100倍,极大拖慢性能。所以,如果无必要,无须理会本变量。 | |-----| # $caching[缓存变量] This tells Smarty whether or not to cache the output of the templates to the $cache_dir. By default this is set to the constant Smarty::CACHING_OFF. If your templates consistently generate the same content,it is advisable to turn on $caching, as this may result in significant performance gains. You can also have multiple caches for the same template. A constant value of Smarty::CACHING_LIFETIME_CURRENT or Smarty::CACHING_LIFETIME_SAVED enables caching. A value of Smarty::CACHING_LIFETIME_CURRENT tells Smarty to use the current $cache_lifetime variable to determine if the cache has expired. A value of Smarty::CACHING_LIFETIME_SAVED tells Smarty to use the $cache_lifetime value at the time the cache was generated. This way you can set the $cache_lifetime just before fetching the template to have granular control over when that particular cache expires. See also isCached(). If $compile_check is enabled, the cached content will be regenerated if any of the templates or config files that are part of this cache are changed. If $force_compile is enabled, the cached content will always be regenerated. See also $cache_dir, $cache_lifetime, $cache_handler_func,$cache_modified_check, is_cached() and the caching section. 本变量用以告诉Smarty是否缓存模板的输出,默认情况下,它将常量设置为Smarty::CACHING_OFF,即否。如果模板内容冗余、重复,建议打开缓存,这样有利于获得良好的性能增益。 你也可以为同一模板设置多个缓存。 译注 | 本变量设置有所升级,使用了类静态常量,请看好说明。 | |-----| Smarty::CACHING_LIFETIME_CURRENT或Smarty::CACHING_LIFETIME_SAVED常量值允许缓存; Smarty::CACHING_LIFETIME_CURRENT常量值告诉Smarty使用当前[$cache_lifetime](#)变量判断缓存是否过期; 当缓存生成时,常量Smarty::CACHING_LIFETIME_SAVED告诉Smarty使用[$cache_lifetime](#)值;用这种方式可以在获取模板之前设置缓存生存时间,以便较精确地控制缓存何时失效。参考[isCached()](#)函数; 如果开启[$compile_check](#)编译检查,当模板或配置文件改变时,将会重新生成缓存; 如果开启[$force_compile](#)强制编译,每次都重新生成缓存内容; 参见[$cache_dir](#)、[$cache_lifetime](#)、$[cache_handler_func](#)、$[cache_modified_check](#)、[ is_cached()](#)和[缓存](#)一节。 # $cache_dir[缓存目录变量] This is the name of the directory where template caches are stored. By default this is ./cache, meaning that Smarty will look for the cache/ directory in the same directory as the executing php script. This directory must be writeable by the web server, see install for more info. You can also use your own custom cache handler function to control cache files, which will ignore this setting. See also $use_sub_dirs. 这是存放模板缓存的目录名,默认情况下为"./cache",也就是说Smarty会在与php执行脚本相同的目录下寻找缓存目录。缓存目录必须可写,参考[安装](#)一节。 你也可以用自己的自定义缓存处理函数来控制缓存文件,它将会忽略上述设置。参考[$use_sub_dirs](#)。 <table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 技术提示 </caption> <tr><td><b>Technical Note: </b> This setting must be either a relative or absolute path. include_path is not used for writing files.<br/> 本设置必须是一个相对或绝对路径,包含路径不可用于写文件。</td> </tr></table> <table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 技术提示 </caption> <tr><td><b>Technical Note: </b> It is not recommended to put this directory under the web server document root.<br/> 不推荐将此目录放在web服务器根目录下。</td> </tr></table> 参考[$caching](#)、[$use_sub_dirs](#)、[$cache_lifetime](#)、[$cache_handler_func](#)、[$cache_modified_check](#)和[缓存](#)一节。 # $cache_lifetime[缓存生存时间变量] This is the length of time in seconds that a template cache is valid. Once this time has expired, the cache will be regenerated. $caching must be turned on (either SMARTY_CACHING_LIFETIME_CURRENT or SMARTY_CACHING_LIFETIME_SAVED) for $cache_lifetime to have any purpose. A value of -1 will force the cache to never expire. A value of 0 will cause the cache to always regenerate (good for testing only, to disable caching a more efficient method is to set [$caching](#) = SMARTY_CACHING_OFF). If you want to give certain templates their own cache lifetime, you could do this by setting $caching=SMARTY_CACHING_LIFETIME_SAVED, then set $cache_lifetime to a unique value just before calling display() or fetch(). If $force_compile is enabled, the cache files will be regenerated every time, effectively disabling caching. You can clear all the cache files with the clear_all_cache() function, or individual cache files (or groups) with the clear_cache() function. 本变量定义模板缓存有效时间(单位秒),一旦这个时间失效,则缓存将会重新生成。 使用$cache_lifetime前必须先开启[$caching](#)(可以设置SMARTY_CACHING_LIFETIME_CURRENT或SMARTY_CACHING_LIFETIME_SAVED); 本变量值设为-1时,将强迫缓存永不过期; 0值将导致缓存总是重新生成(仅有利于测试,一个更有效的使缓存无效的方法是设置[$caching](#) = false) ; 如果你需要为每个模板设置自己的缓存有效时间,可以使用$caching=SMARTY_CACHING_LIFETIME_SAVED,然后再调用[display()](#)或[fetch()](#)函数前设置$caching_lifetime为唯一值。 如果启动了强迫编译[$force_compile](#),则缓存文件每次将会重新生成。要想有效地停止缓存,你可以利用[clear_all_cache()](#) 函数清除所有的缓存文件,或者利用[clear_cache()](#) 函数清除个别文件(或文件组)。 # $cache_handler_func[缓存处理函数变量] You can supply a custom function to handle cache files instead of using the built-in method using the $cache_dir. See the custom cache handler function section for details. 你可以提供一个自定义函数来处理缓存文件,而不通过变量[$cache_dir](#)使用Smarty内置方法。详见:[缓存处理函数](#)一节。 # $cache_modified_check[缓存修改检查变量] If set to true, Smarty will respect the If-Modified-Since header sent from the client. If the cached file timestamp has not changed since the last visit, then a "304 Not Modified" header will be sent instead of the content. This works only on cached content without {insert} tags. 如果设置该变量为真,Smarty将分析客户端发送来的If-Modified-Since头信息。如果缓存文件时间戳自上次访问以来没有改变,则发送一个"304 Not Modified"头,而不是缓存文件内容。这种方式仅工作于没有[{insert}](#)标记的缓存内容。 # $config_overwrite[配置.覆盖变量] If set to TRUE, the default then variables read in from config files will overwrite each other. Otherwise,the variables will be pushed onto an array. This is helpful if you want to store arrays of data in config files,just list each element multiple times. 如果本变量设为真,则从配置文件中读取出来的变量将会互相覆盖,否则,变量将会去堆压到一个数组中。如果你想把配置文件中的数据存储到数组里,这种方式是很有用的,可以多次列出里面的元素(见下例)。默认情况下,设为真。 **Example 12.4. Array of config #variables# 例 12-4.配置文件变量****数组** <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><p>This examples uses {cycle} to output a table with alternating red/green/blue row colors with $config_overwrite = FALSE.<br/> 本例设置$config_overwrite = FALSE,使用{cycle}输出交替显示红/绿/蓝行颜色的表格。</p> <p><br/> The config file.</p> <p> # row colors<br/> rowColors = #FF0000<br/> rowColors = #00FF00<br/> rowColors = #0000FF<br/><br/> The template with a {section} loop.</p> <p><br/> &lt;table&gt;<br/> {section name=r loop=$rows}<br/> &lt;tr bgcolor="{cycle values=#rowColors#}"&gt;<br/> &lt;td&gt; ....etc.... &lt;/td&gt;<br/> &lt;/tr&gt;<br/> {/section}<br/> &lt;/table&gt;</p></td> </tr></table> 参见[{config_load}](#)、[get_config_vars()](#)、[clear_config()](#)、[config_load()](#)和[配置文件](#)一节。 # $config_booleanize[配置.布尔型变量] If set to TRUE, config files values of on/true/yes and off/false/no get converted to boolean values automatically. This way you can use the values in the template like so: {if #foobar#}...{/if}. If foobar was on, true or yes, the {if} statement will execute. Defaults to TRUE. 如果本变量设为真,配置文件中的on/true/yes和off/false/no值会自动转化为布尔值。这样的话,你就可以在模板中像{if #foobar#} ... {/if}这样使用上述符合布尔类型的值了。如果foobar为on、true或yes,那么 {if}语句就会执行了。默认情况下,该变量值为真。 # $config_read_hidden[配置.读取隐藏变量] If set to true, hidden sections (section names beginning with a period) in config files can be read from templates. Typically you would leave this false, that way you can store sensitive data in the config files such as database parameters and not worry about the template loading them. false by default. 如果设本变量为真,可以从模版中读取在配置文件中的隐藏节块(以英文句号/句点开头的节块,见[配置文件](#)一节)。一般情况下,你会保留本变量为假,这样你可以在配置文件里存放敏感数据,例如数据库参数,而不用担心模版会将它们调用出来。默认情况下,本变量设为假。 # $config_fix_newlines[配置.固定换行符变量] If set to TRUE, mac and dos newlines ie '\r' and '\r\n' in config files are converted to '\n' when they are parsed. Default is TRUE. 如果本变量设为真,那么在配置文件中的mac和dos换行符(即\r和\r\n)在语法解析时将会转换为\n。默认情况下,该变量为真。 # $default_template_handler_func[默认模板处理函数变量] This function is called when a template cannot be obtained from its resource. 该函数在不能从模板的源目录下获取模板时会得到调用。 # $php_handling[php处理变量] This tells Smarty how to handle PHP code embedded in the templates. There are four possible settings,the default being Smarty::PHP_PASSTHRU. Note that this does NOT affect php code within {php}{/php} tags in the template. - Smarty::PHP_PASSTHRU - Smarty echos tags as-is. - Smarty::PHP_QUOTE - Smarty quotes the tags as html entities. - Smarty::PHP_REMOVE - Smarty removes the tags from the templates. - Smarty::PHP_ALLOW - Smarty will execute the tags as PHP code. 该变量告诉Smarty怎样处理嵌入到模版中的php代码。有四种可能的设置,默认为Smarty::PHP_PASSTHRU。注意,本变量的设置不会影响模板里面{php}{/php}标记中的php代码。 Smarty::PHP_PASSTHRU--原样输出标记 Smarty::PHP_QUOTE--作为html实体引用标记 Smarty::PHP_REMOVE--从模板中移出标记 Smarty::PHP_ALLOW --将作为php代码执行标记 <table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 提示 </caption> <tr><td>NOTE: Embedding PHP code into templates is highly discouraged. Use <a href="language.custom.functions.html">custom functions</a> or <a href="language.modifiers.html">modifiers</a> instead.<br/> 极度不赞成将php代码嵌入到模板中,建议改用<a href="language.custom.functions.html">自定义函数</a>或<a href="language.modifiers.html">调节器</a>。</td> </tr></table> # $trusted_dir[信任目录变量] $trusted_dir is only for use when $security is enabled. This is an array of all directories that are considered trusted. Trusted directories are where you keep php scripts that are executed directly from the templates with [{include_php}](#). $trusted_dir仅用在$security启动之后。本变量是一个数组,它包含所有被认为能信任的目录。你可以将php脚本放到这些受信任目录中,这些脚本会直接在模板里以[{include_php}](#)标记得到执行。 # $left_delimiter[左定界符变量] This is the left delimiter used by the template language. Default is "{". See also $right_delimiter and escaping smarty parsing . 本变量定义模板中的左定界符,默认为"{"。 参见[$right_delimiter](#)和[忽略smarty解析](#)。 # $right_delimiter[右定界符变量] This is the right delimiter used by the template language. Default is "}". See also $left_delimiter and escaping smarty parsing. 本变量定义模板中的右定界符,默认为"}"。 参见[$left_delimiter](#)和[忽略smarty解析](#)。 # $compiler_class[编译类变量] Specifies the name of the compiler class that Smarty will use to compile the templates. The default is 'Smarty_Compiler'. For advanced users only. 本变量用以指定Smarty编译模板的编译类名,默认为:'Smarty_Compiler'。仅适合于高级用户。 # $request_vars_order[变量顺序变量] The order in which request variables are registered, similar to variables_order in php.ini See also $smarty.request and $request_use_auto_globals. 本变量用以配置注册的请求变量顺序,类似于php.ini中的variables_order。 参见[$smarty.request](#)和[$request_use_auto_globals](#)。 # $request_use_auto_globals[自动全局变量] Specifies if Smarty should use PHP's $HTTP_*_VARS[] when FALSE or $_*[] when TRUE which is the default value. This affects templates that make use of {$smarty.request.*}, {$smarty.get.*} etc. 本变量指定Smarty是否使用php的$HTTP_*_VARS[]数组变量(默认$request_use_auto_globals=false)或$_*[]数组($request_use_auto_globals=true)变量。这对使用了{$smarty.request.*}、{$smarty.get.*}等标记的模板有影响。 <table width="80%" border="0" cellpadding="2" cellspacing="2" class="warning"><caption> 警告 </caption> <tr><td>Caution<br/> If you set $request_use_auto_globals to true, $request_vars_order has no effect but PHP's configuration value gpc_order is used.<br/> 如果设置$request_use_auto_globals为真,<a href="#request_vars_order">$request_vars_order</a>就无效了,但php的gpc_order配置值还有作用。</td> </tr></table> 译注 | $HTTP_*_VARS[]已在php5中弃用,改而使用$_*[]超全局变量,请注意自己的php版本。 | |-----| # $compile_id[编译id变量] Persistant compile identifier. As an alternative to passing the same $compile_id to each and every function call, you can set this $compile_id and it will be used implicitly thereafter. With a $compile_id you can work around the limitation that you cannot use the same $compile_dir for different $template_dirs. If you set a distinct $compile_id for each $template_dir then Smarty can tell the compiled templates apart by their $compile_id. If you have for example a prefilter that localizes your templates (that is: translates language depend parts) at compile time, then you could use the current language as $compile_id and you will get a set of compiled templates for each language you use. Another application would be to use the same compile directory across multiple domains / multiple virtual hosts. 本变量是个固定的编译标识。作为一个可选办法将相同的编译标识传递给每个函数调用,你可以设置这个编译id,此id将会在后台暗中被调用。 编译id的使用限定了工作范围,同一编译id不能对应不同的[$template_dirs](#)模板地址。如果为一个$template_dirs设置不同的编译id,那么Smarty会依$compile_id将这些编译模板区分开。 假如,你有一个预过滤器在编译时本地化你的模板(翻译的语言取决于所在地区),那么你可以使用当前语言作为$compile_id,这样你就得到了一套依语言分类的编译模板。 本变量的另一应用是使用多个域名/多个虚拟主机访问同一编译目录。 **Example 12.5. $compile_id in a virtual host environment 例 12-4.$compile_id****在虚拟主机环境的设置** <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td>&lt;?php<br/> $smarty-&gt;compile_id = $_SERVER['SERVER_NAME'];<br/> $smarty-&gt;compile_dir = '/path/to/shared_compile_dir';<br/> ?&gt;</td> </tr></table> # $use_sub_dirs[子目录变量] Smarty will create subdirectories under the compiled templates and cache directories if $use_sub_dirs is set to TRUE, default is FALSE. In an environment where there are potentially tens of thousands of files created, this may help the filesystem speed. On the other hand, some environments do not allow PHP processes to create directories, so this must be disabled which is the default. Sub directories are more efficient, so use them if you can. Theoretically you get much better perfomance on a filesystem with 10 directories each having 100 files, than with 1 directory having 1000 files. This was certainly the case with Solaris 7 (UFS)... with newer filesystems such as ext3 and especially reiserfs,the difference is almost nothing. 如果$use_sub_dirs变量设为真,Smarty将会在编译模板和缓存目录下创建子目录,默认为假。在拥有成千上万的文件环境中,本变量为真可以加速你的文件系统。另一方面,存在一些服务器环境下不允许php进程建立目录的情况,那么,就保持此变量为假。 子目录非常有效,因此,尽可能使用它吧。理论上来说,在一个文件系统中,10个目录分别放置100个文件比1个目录放置1000个文件的性能要好。当然这是Solaris 7操作系统的情况...在新的文件系统中,如ext3,尤其是ReiserFS文件系统,这种差别可以忽略不计。 <table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 技术提示 </caption> <tr><td><p>Technical Note </p><li>$use_sub_dirs=true doesn't work with safe_mode=On [http://php.net/features.safe-mode], that's why it's switchable and why it's off by default.</li> <li> $use_sub_dirs=true on Windows can cause problems.</li> <li> Safe_mode is being deprecated in PHP6.</li> <br/><li>当Smarty工作在php安全模式时(safe_mode=on),$use_sub_dirs=true将失效,这就是为什么本变量须设切换开关和默认为关闭的原因;</li> <li> 在windows系统中,$use_sub_dirs=true会出错;</li> <li> 安全模式在php6中已被弃用。</li></td> </tr></table> 参见[$compile_id](#)、[$cache_dir](#)和[$compile_dir](#)。 # $default_modifiers[默认调节器变量] This is an array of modifiers to implicitly apply to every variable in a template. For example, to HTML-escape every variable by default, use array('escape:"htmlall"'). To make a variable exempt from default modifiers, pass the special smarty modifier with a parameter value of nodefaults modifier to it, such as {$var|smarty:nodefaults}. 本变量是个调节器数组,它隐式作用于模板中的每个变量,例如,默认为HTML转义每个变量,可用数组('escape:"htmlall"')。如果不使用默认调节器,可传递其它smarty调节器给本变量,调节器名后须带一个参数值“nodefaults”,例如,{$var|smarty:nodefaults}。 # $default_resource_type[默认源类型变量] This tells smarty what resource type to use implicitly. The default value is 'file', meaning that $smarty->display('index.tpl'); and $smarty->display('file:index.tpl'); are identical in meaning. See the [resource](#) chapter for details. 本变量告诉smarty隐式地使用什么资源类型。默认值为'file',也就是说,$smarty->display('index.tpl')和$smarty->display('file:index.tpl')从意思上是一样的。 参见[resource资源](#)一章。 <table width="100%" summary="Footer navigation table" border="0" cellpadding="0" cellspacing="0"><tr><td width="33%" align="left" valign="top"><a accesskey="P" href="smarty.constants.html">Prev</a></td><td width="34%" align="center"><a accesskey="H" href="index.html">Home</a></td><td width="33%" align="right" valign="top"> <a href="api.functions.html">Next</a></td></tr><tr><td width="33%" align="left" valign="top">SMARTY_DIR<br/> Smarty 目录</td><td width="34%" align="center"><a accesskey="U" href="smarty.for.programmers.html">Up</a></td><td width="33%" align="right" valign="top">Smarty Class Methods()<br/> Smarty类方法</td></tr></table>