如果你的問題是{php}可以直接向下翻看加粗下面的
更新一下: 突然發(fā)現(xiàn)<% %>
這種定界符可能是被解析成asp的致燥,而PHP7刪除了asp_tags
這個ini配置,且移除了這兩個標(biāo)簽豹芯,不太確定是不是這個影響的宅粥,有兄弟知道麻煩回復(fù)下龟劲。
最近公司升級PHP7.4,突然發(fā)現(xiàn)用到smarty的模板都不顯示了益缠,且不報任何錯誤税手,$smarty->display()
上下都能正常輸出,這就很奇怪了条篷,我們用的版本是2.6.14
,查了一下網(wǎng)上說是目錄權(quán)限的問題骗随,于是把目錄權(quán)限都改成了777還是不行蛤织,自己本地PHP7.3環(huán)境寫個demo測試沒問題,抱著試一試的態(tài)度把站點的smarty換成了最新的3.1.36
, 真的成了鸿染,剩下的就是語法報錯的問題了指蚜,這下就好辦了,下面說下smarty 2 到 3影響最大的幾個地方~~
- PHP版本
== PHP Version ==
Smarty 3 is PHP 5 only. It will not work with PHP 4.
不再支持PHP4 現(xiàn)在不會還有網(wǎng)站跑在PHP4下吧涨椒,不會吧
- 定界符和空格
== Delimiters and whitespace ==
Delimiters surrounded by whitespace are no longer treated as Smarty tags.
Therefore, { foo } will not compile as a tag, you must use {foo}. This change
Makes Javascript/CSS easier to work with, eliminating the need for {literal}.
This can be disabled by setting $smarty->auto_literal = false;
翻譯過來就是 {空格if}這樣的標(biāo)簽是無法解析的摊鸡,如果你以前沒有注意這方面的問題那可要注意了,因為我們的站點都是這樣的 有的定界符帶空格 有的不帶蚕冬,結(jié)果都是各種標(biāo)簽不閉合報錯免猾,
當(dāng)然經(jīng)過我測試 這個沒有官方說的這么嚴格,只要左定界符后沒有空格就行了囤热,你只需要在你項目中全局替換一下就好了
- 沒有雙引號的字符串
== 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">
{include file='path/foo.tpl'}
</source>
就是字符串要帶雙引號猎提,這個犯錯的地方應(yīng)該不多吧,大部分語言字符串一般都是引號包起來的旁蔼,大家都習(xí)慣寫引號锨苏,遇到改一下就好了
- php 標(biāo)簽
The {php} tag is no longer available in the standard Smarty calls.
The use of {php} tags is deprecated and only available in the SmartyBC class.
這算是最明顯的變化了,{php}直接就不能用了棺聊,想用的話就得用這個向下兼容的類 SmartyBC 伞租,
引入Smarty.class.php
,實例化就能直接用了,這里面還有一個坑限佩,等下再說葵诈。
- include_php標(biāo)簽
== {include_php} Tag ==
The {include_php} tag is no longer available in the standard Smarty calls.
The use of {include_php} tags is deprecated and only available in the SmartyBC class.
這個和{php}標(biāo)簽一樣,用這個向下兼容的類 SmartyBC
- 插件文件名
== Plugin Filenames ==
Smarty 3 optionally supports the PHP spl_autoloader. The autoloader requires filenames
to be lower case. Because of this, Smarty plugin file names must also be lowercase.
In Smarty 2, mixed case file names did work.
smarty3的自動加載只能試別小寫祟同,所以以前插件名大小寫混用可能會失效
其他的變化就不多說了 驯击,文件包里都有文件說明,里面都寫了耐亏,雖然有幾個地方是錯的 :)
重點部分
下面著重說下{php}這個標(biāo)簽
== {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.
But if you scatter PHP code which belongs together into several
{php} tags it may not work any longer.
$smarty->allow_php_tag=true
就是我說錯的地方,可能也不是錯,官方哪個版本改了也有可能
如果你發(fā)現(xiàn)你該干的都干了 {php}標(biāo)簽不生效或者里面的內(nèi)容不生效可以考慮按找我下面說的測試下
- 開啟 allow_php_tag = true
- php_handling設(shè)置成 Smarty::PHP_ALLOW (默認是0)
- 更換定界符測試
上面的設(shè)置用我下面的代碼
$tpl = new SmartyBC;
$my_security_policy = new Smarty_Security($tpl);
$my_security_policy->allow_php_tag = true;
$my_security_policy->php_functions = array();
$my_security_policy->php_handling = Smarty::PHP_ALLOW;
$my_security_policy->php_modifiers = array();
$my_security_policy->streams = array();
$smarty->enableSecurity($my_security_policy);
下面說下我遇到的最坑的問題
我們的定界符是 <%
%>
,這個有啥問題我貼個github上的isssu吧
https://github.com/smarty-php/smarty/issues/445
除了改代碼基本無解 沪斟,這可太坑了
我們設(shè)計的文件太多了广辰,改代碼我可能會死,于是我想這看看能不能換版本搞定主之,這一換還真成了择吊,smarty3換了幾個版本都不行,但是我發(fā)現(xiàn)了一個在smarty3后還更新了的2.6.31
,這個版本可以跑槽奕,完美了除了語法棄用几睛,沒什么大問題。
分享下這個版本 https://github.com/smarty-php/smarty/releases/tag/v2.6.31
一下午的研究的東西基本都在這篇文章里了粤攒,希望對你有所幫助