3

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
  • was searching around, found this, you might be able to adapt : qfox.nl/notes/79 Aug 15, 2012 at 20:24
  • He's trying to solve a different problem, because in v2 the plain php syntax works in an {if} block, but precompiling is an idea I had not thought of. I'd still have to change the templates for matching purposes, but I'll look into it further. Thanks.
    – undefined
    Aug 15, 2012 at 20:32

2 Answers 2

0

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.

0

My understanding is that those constants will always be interpreted as strings in v3.

Wherever you define those constants, use

$smarty->assign( 'constant', CONSTANT );

Either way, you are going to need to modify your code if you want to upgrade. Perhaps you should weigh the pros/cons of the v2->v3 update and go from there.

1
  • Thanks. The main pro that we're after is template inheritance, something I've always missed from Django.
    – undefined
    Aug 15, 2012 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.