🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
<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="resources.html" accesskey="P">Prev</a></td><td width="50%" align="center" valign="bottom">Smarty3.0.x Installation Package</td><td width="25%" align="right" valign="bottom"><a href="smarty3.readme.html">Next</a></td></tr></table> # SMARTY2_BC_NOTES[2.x版本升级至3.x版本的提示] Smarty 2 and Smarty 3 are quite similar in implementation, but do have a few differences you need to be aware of when upgrading from Smarty 2 to Smarty 3. = Known incompatibilities with Smarty 2 = == Syntax == The Smarty 3 API has been updated in various places. Some Smarty 2 API calls need to be updated to comply with Smarty 3. You will get a deprecation notice with old Smarty 2 API calls, and informed what the new one is. See the README that comes with Smarty 3 for more information. The {$array|@mod} syntax has always been a bit confusing, where an "@" is required to apply a modifier to an array instead of the individual array elements. Normally you always want the modifier to apply to the variable regardless of its type. In Smarty 3,{$array|mod} and {$array|@mod} behave identical. It is safe to drop the "@" and the modifier will still apply to the array. If you really want the modifier to apply to each array element, you must loop the array in-template, or use a custom modifier that supports array iteration. Most smarty functions already escape array elements where necessary such as {html_options} == PHP Version == Smarty 3 is PHP 5 only. It will not work with PHP 4. == {php} Tag == The {php} tag is disabled by default. The use of {php} tags is deprecated. It can be enabled with $smarty-allow_php_tag=true. Variables inside {php} blocks no longer share scope with other {php} blocks on the page, so be aware of this change if you use them. == Delimiters and whitespace == Smarty delimiters {} surrounded by whitespace are no longer treated as Smarty tags.Therefore, { foo } will be ignored by Smarty, but {foo} is recognized. This change makes Javascript/CSS easier to work with, eliminating the need for {literal}. This feature can be disabled by setting $smarty->auto_literal = false; == Unquoted Strings == Smarty 2 was a bit more forgiving (and ambiguous) when it comes to unquoted strings in parameters. Smarty3 is more restrictive. You can still pass strings without quotes so long as they contain no special characters. (anything outside of A-Za-z0-9_) For example filename strings must be quoted: <source lang="smarty"> {assign var=foo value=baz} <-- works ok {include file="path/foo.tpl"} <-- needs quotes! </source> == Extending the Smarty class == Smarty 3 follows standard PHP5 constructor rules. When extending the Smarty class, use __construct() as the class constructor name. If you implement your own constructor,be certain to call parent::__construct() first. <source lang="php"> class MySmarty extends Smarty { function __construct() { parent::__construct(); // your initialization code goes here } } </source> == Autoloader == Smarty implements its own autoloader with spl_autoload_register. If you use an autoloader in your own application, you MUST register yours as well. Using__autoload() WILL FAIL. This is standard PHP5 autoloader procedure for shared libraries. See http://us3.php.net/manual/en/function.spl-autoload-register.php == Plugin Filenames == Since Smarty 3 uses the default spl_autoloader, the plugin filenames are now required to be lower case. Smarty 2 allowed mixed case plugin names, you must rename them for Smarty 3. == Scope of Special Smarty Variables == In Smarty 2 the special Smarty variables $smarty.section.* and $smarty.foreach.* had global scope. If you had loops with the same name in subtemplates, you could accidentally overwrite values of a parent template. In Smarty 3 these special Smarty variables now have local scope in the template which is defining the loop. In the rare case you need these values in a subtemplate, you have to pass them as parameters. : <source lang="smarty"> {include file="path/foo.tpl" index=$smarty.section.foo.index} </source> == SMARTY_RESOURCE_CHAR_SET == Smarty 3 sets the constant SMARTY_RESOURCE_CHAR_SET to utf-8 as the default template charset. This is now used with modifiers like escape as the default charset. If your templates use another charset, make sure that you define the constant accordingly. == trigger_error() == The API function trigger_error() has been removed. It is still included in the Smarty2 API wrapper. == Smarty constants == The constants SMARTY_PHP_PASSTHRU SMARTY_PHP_QUOTE SMARTY_PHP_REMOVE SMARTY_PHP_ALLOW have been replaced with class constants Smarty::PHP_PASSTHRU Smarty::PHP_QUOTE Smarty::PHP_REMOVE Smarty::PHP_ALLOW Smarty2和Smarty2的执行非常相似,但当由2.x升级到3.x时仍需要清楚它们间的些许不同点。 =与Smarty2的不兼容部份= **==法语==** Smarty3 API在各个位置都升了级,一些Smarty2 API的调用需要完全升级到3,调用Smarty2的API会得到一个警告,同时它会通知该用哪个新的API,参考Smarty3中附带的README阅读更多信息。 {$array|@MOD}语法一直有点混乱,其中“@”是一个运用到数组而不是一个单个数组元素的修正符。通常情况下,你总是希望调节器适用于各种类型的变量。在Smarty3,{$array|mod}和{$array|@mod}的行为相同,去掉@是安全的,而调节器将仍作用于数组。如果你想调节器适用于每个数组元素,你必须在模板中循环数组,或使用支持数组迭代的自定义调节器。大多数Smarty方法已经转义了必要的数组元素,如[{html_options}](#)。 **==PHP 版本==** Smarty3只适用于php5,不再支持php4。 **=={php}标签==** {php}默认情况下关闭,不赞成使用{php}标签,如果确实需要可以设置$marty->allow_php_tag = true。在页面中,一个{php}语句块里的变量已经不能与其它{php}语句块中共享使用,使用时请注意这些变化。 **==分隔符和空格==** 在Smarty分隔符{}里左右出现空格符时将不再当作Smarty标签对待,因此{ foo }会被Smarty忽略,但{foo}则可识别。这个改变是为javascript/css让路,但上述改变不包括必须用到的{literal},这个特性可用设置$smarty->auto_literal = false去掉。 **==不加引号的字符串==** Smarty2处理加引号的字符串参数时比较随意(或说比较含糊)。 Smarty3则更多限制。您仍然可以通过不带引号的字符串,只要它们不包含任何特殊字符。(特殊字符指除A-Za-z0-9_外的符号)。 例如,文件名字符串必须带引号: | ~~~ <source lang="smarty"> {assign var=foo value=baz} <-- works ok 不带引号正常工作 {include file="path/foo.tpl"} <-- needs quotes! 需要引号</source> ~~~ | |-----| **==扩展Smarty类==** Smarty3 沿用标准的php5构造规则。当扩展Smarty类时,用__construct()作为类的构造函数名,如果使用你自己的构造器,必须先声明parent::__construct()。 <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><p>&lt;source lang="php"&gt;<br/> class MySmarty extends Smarty {<br/> function __construct() {<br/> parent::__construct();</p> <p>// your initialization code goes here 这里放置你自己的初始化代码</p> <p>}<br/> }<br/> &lt;/source&gt;</p> </td> </tr></table> **==自动加载器==** Smarty用spl_autoload_register接口自己的自动加载器,如果你在应用程序中用自己的自动加载器,必须先注册它们,用__autoload()会失败,这是标准php5共享库的自动加载器程序。 参见[http://us3.php.net/manual/en/function.spl-autoload-register.php ](http://us3.php.net/manual/en/function.spl-autoload-register.php) **==插件文件名==** 此从Smarty3使用默认的spl_autoloader,插件文件名需要小写,Smarty2允许用大小写混写插件文件名,在smarty3时代必须改写这类命名。 **==特殊Smarty变量的作用域==** 在Smarty2,特殊Smarty变量$smarty.section.*和$smarty.foreach.*有全局域,如果你在子模板中循环了同名变量,可能会意外覆盖父模版的值。 在Smarty3中,这些特殊的Smarty变量在定义的模板循环中只有局部的作用域,在极少数情况下,你需要从子模板获取这些特殊值,必须通过设定参数方法传递这些值: | ~~~ <source lang="smarty"> {include file="path/foo.tpl" index=$smarty.section.foo.index}</source> ~~~ | |-----| **== SMARTY_RESOURCE_CHAR_SET ==** Smarty3设置常量SMARTY_RESOURCE_CHAR_SET为utf-8作为默认模版的编码,如今,它已用作如转义等调节器的默认编码,如果你的模版想用其它编码,请确认你已经相应地定义了这个常量。 **== trigger_error() ==** API函数trigger_error()已被移除,但它仍存在Smarty2的API包装器中。 **==Smarty 常量==** 常量 SMARTY_PHP_PASSTHRU SMARTY_PHP_QUOTE SMARTY_PHP_REMOVE SMARTY_PHP_ALLOW 已被下面这些类常量替换 Smarty::PHP_PASSTHRU Smarty::PHP_QUOTE Smarty::PHP_REMOVE Smarty::PHP_ALLOW <table summary="Footer navigation table" width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td width="33%" align="left" valign="top"><a href="resources.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="smarty3.readme.html">Next</a></td></tr><tr><td width="33%" align="left" valign="top">Smarty3.0.x Installation Package<br/> 3.0安装包</td><td width="34%" align="center" valign="top"><a href="appendixes.html" accesskey="U">Up</a></td><td width="33%" align="right" valign="top">README<br/> 3.0.x使用指南</td></tr></table>