<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><source lang="php"><br/> class MySmarty extends Smarty {<br/> function __construct() {<br/> parent::__construct();</p> <p>// your initialization code goes here 這里放置你自己的初始化代碼</p> <p>}<br/> }<br/> </source></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>
- Smarty模板編譯引擎
- 序
- 譯序
- I.開始
- 第一章. 什么是Smarty?
- 第二章. 安裝
- II.模板設計者篇
- 第三章.基本語法
- 注釋
- 變量
- 函數
- 屬性
- 雙引號里嵌入變量
- 數學運算
- 忽略Smarty解析
- 第四章.變量
- 從PHP分配的變量
- 從配置文件讀取的變量
- 變量范圍
- {$smarty}保留變量
- 第五章.變量調節器
- capitalize
- cat
- count_characters
- count_paragraphs
- count_sentences
- count_words
- date_format
- default
- escape
- indent
- lower
- nl2br
- regex_replace
- replace
- spacify
- string_format
- strip
- strip_tags
- truncate
- upper
- wordwrap
- 第六章.組合修改器
- 第七章.內置函數
- {$var=}
- {append}
- {assign}
- {block}
- {call}
- {capture}
- {config_load}
- {debug}
- {extends}
- {for}
- {foreach},{foreachelse}
- @index
- {function}
- {if},{elseif},{else}
- {include}
- {include_php}
- {insert}
- {ldelim},{rdelim}
- {literal}
- {nocache}
- {php}
- {section},{sectionelse}
- .index
- {while}
- 第八章.自定義函數
- {counter}
- {cycle}
- {eval}
- {fetch}
- {html_checkboxes}
- {html_image}
- {html_options}
- {html_radios}
- {html_select_date}
- {html_select_time}
- {html_table}
- {mailto}
- {math}
- {textformat}
- 第九章.配置文件
- 第十章.調試控制臺
- III.模板程序員篇
- 第十一章 常量
- SMARTY_DIR
- 第十二章 Smarty類變量
- $template_dir
- 第十三章.Smarty類方法
- append()
- appendByRef()
- assign()
- assignByRef()
- clearAllAssign()
- clearAllCache()
- clearAssign()
- clearCache()
- clearCompiledTpl()
- clearConfig()
- compileAllConfig()
- compileAllTemplates()
- configLoad()
- createData()
- createTemplate()
- disableSecurity()
- display()
- enableSecurity()
- fetch()
- getConfigVars()
- getRegisteredObject()
- getTags()
- getTemplateVars()
- isCached()
- loadFilter()
- registerFilter()
- registerPlugin()
- registerObject()
- registerResource()
- templateExists()
- unregisterFilter()
- unregisterPlugin()
- unregisterObject()
- unregisterResource()
- testInstall()
- 第十四章.緩存
- 建立緩存
- 多重緩存
- 緩存集合
- 控制插件輸出的可緩存性
- 第十五章.高級特性
- 安全
- 通過模板更改設置
- 模板繼承
- 數據流
- 對象
- 靜態類
- 預過濾器
- 后過濾器
- 輸出過濾器
- 緩存處理函數
- 資源
- 第十六章.以插件擴展Smarty
- 插件如何工作
- 命名約定
- 編寫插件
- 模板函數
- 調節器
- 塊函數
- 編譯函數
- 預濾器/后濾器
- 輸出過濾器
- 資源
- 插入
- Ⅳ.附錄
- 第十七章.疑難解答
- Smarty/PHP 錯誤
- 第十八章.使用技巧和經驗
- 空白變量處理
- 默認變量處理
- 傳遞變量標題給頭模板
- 日期
- WAP/WML
- 組件化模板
- 拒絕電子郵件地址
- 第十九章. 相關資源
- 第二十章. 漏洞
- 3.0安裝包
- 2.x版本升級至3.x版本的提示
- 3.0.x使用指南
- 翻譯人員列表