美文网首页
smarty升级注意事项及经验分享

smarty升级注意事项及经验分享

作者: Mr_dreamer | 来源:发表于2020-06-12 18:16 被阅读0次

    如果你的问题是{php}可以直接向下翻看加粗下面的

    更新一下: 突然发现<% %>这种定界符可能是被解析成asp的,而PHP7删除了asp_tags这个ini配置,且移除了这两个标签,不太确定是不是这个影响的,有兄弟知道麻烦回复下。

    最近公司升级PHP7.4,突然发现用到smarty的模板都不显示了,且不报任何错误,$smarty->display()上下都能正常输出,这就很奇怪了,我们用的版本是2.6.14,查了一下网上说是目录权限的问题,于是把目录权限都改成了777还是不行,自己本地PHP7.3环境写个demo测试没问题,抱着试一试的态度把站点的smarty换成了最新的3.1.36, 真的成了,剩下的就是语法报错的问题了,这下就好办了,下面说下smarty 2 到 3影响最大的几个地方~~

    1. PHP版本

    == PHP Version ==
    Smarty 3 is PHP 5 only. It will not work with PHP 4.
    不再支持PHP4 现在不会还有网站跑在PHP4下吧,不会吧

    1. 定界符和空格

    == 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}这样的标签是无法解析的,如果你以前没有注意这方面的问题那可要注意了,因为我们的站点都是这样的 有的定界符带空格 有的不带,结果都是各种标签不闭合报错,
    当然经过我测试 这个没有官方说的这么严格,只要左定界符后没有空格就行了,你只需要在你项目中全局替换一下就好了

    1. 没有双引号的字符串

    == 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>

    就是字符串要带双引号,这个犯错的地方应该不多吧,大部分语言字符串一般都是引号包起来的,大家都习惯写引号,遇到改一下就好了

    1. php 标签

    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,实例化就能直接用了,这里面还有一个坑,等下再说。

    1. include_php标签

    == {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}标签一样,用这个向下兼容的类 SmartyBC

    1. 插件文件名

    == 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}这个标签

    == {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 就是我说错的地方,可能也不是错,官方哪个版本改了也有可能
    如果你发现你该干的都干了 {php}标签不生效或者里面的内容不生效可以考虑按找我下面说的测试下

    1. 开启 allow_php_tag = true
    2. php_handling设置成 Smarty::PHP_ALLOW (默认是0)
    3. 更换定界符测试

    上面的设置用我下面的代码

    $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);
    

    点此查看文档
    php_handling参数说明

    下面说下我遇到的最坑的问题
    我们的定界符是 <% %>,这个有啥问题我贴个github上的isssu吧
    https://github.com/smarty-php/smarty/issues/445
    除了改代码基本无解 ,这可太坑了

    我们设计的文件太多了,改代码我可能会死,于是我想这看看能不能换版本搞定,这一换还真成了,smarty3换了几个版本都不行,但是我发现了一个在smarty3后还更新了的2.6.31,这个版本可以跑,完美了除了语法弃用,没什么大问题。

    分享下这个版本 https://github.com/smarty-php/smarty/releases/tag/v2.6.31

    一下午的研究的东西基本都在这篇文章里了,希望对你有所帮助

    相关文章

      网友评论

          本文标题:smarty升级注意事项及经验分享

          本文链接:https://www.haomeiwen.com/subject/mqjntktx.html