duan19850312 2012-08-15 20:18
浏览 69
已采纳

将Smarty从Smarty v2升级到v3

I am in the process of upgrading a PHP-based site from Smarty2 to Smarty3. I have managed to get most everything working on both versions by replacing require_once('Smarty.class.php' with the following:

@ include_once('SmartyBC.class.php');
if (class_exists('SmartyBC')) {
    $smarty = new SmartyBC();
    //this is for use outside templates.  inside templates, $smarty.version == 'Smarty-3.x.y.z'
    $smartyVersion = 3;
}
//catch (Exception $e) {
else {
    require_once('Smarty.class.php');
    $smarty = new Smarty();
    $smartyVersion = 2;
}

Then I have a symlink at /usr/local/lib/php/Smarty, that points to either version 2 or version 3. In order to swap between the two, I need only change the symlink. By using the compatibility layer class (SmartyBC) almost everything already works with both versions. I'd like to maintain compatibility with both versions until everything has been fully tested, and make as few changes to old templates as possible. With that in mind, here is the piece of code I am trying to upgrade:

//someinclude.php:
define('SOME_CONSTANT', 'someConst');

//somepage.tpl:
{if $dynVal == SOME_CONSTANT}dynamic content{/if}

This doesn't work in V3, but this will:

{if $dynVal == $smarty.const.SOME_CONSTANT}dynamic content{/if}

These constants (quite a few of them) are used all over the place, so ideally I'd like to find a way to make the first way work in version 3. If this in not possible, what is the cleanest possible way to support both versions temporarily?

  • 写回答

2条回答 默认 最新

  • dsuxcxqep31023992 2012-08-15 20:44
    关注

    I had not realized that the change I described in the question also works with version 2:

    {if $dynVal == $smarty.const.SOME_CONSTANT}dynamic content{/if}
    

    I'll hold out to see what other answers come, as I'd still prefer to not have to change the templates if I don't have to, but I think this is likely going to be the cleanest way to make the change. I was worried that I was going to have to do something like this:

    {if ($newSmarty && $dynVal == $smarty.const.SOME_CONSTANT) || (!newSmarty && 'dynVal' == SOME_CONSTANT) }dynamic content{/if}
    

    And nobody wants to maintain that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?