tags:

views:

20

answers:

1

I have the Smarty code

{$obj->foo()->bar()}

and Smarty complains about 'unrecognized tag' in the expression. If I change it to just

{$obj->foo()}

it doesn't complain, so I assume the problem is with the fact that I'm calling a method on the result of a method. Is this a limitation of Smarty's parser, or am I missing something else here?

I know I can work around this with {assign}, I just wanted to know if I'd understood the extent of the limitations correctly.

+1  A: 

Are you using Smarty2 or Smarty3?

Smarty2 will require you to use {assign} after the first method. It will also have problems if you pass more than one argument to a method.

Smarty3 has a completely rewritten parser and you can do exactly what you want. You also no longer need {assign}, as you can simply do {$new_var = "Anything"}.

Here's a test in Smarty3:

Our classes

class Foo {
    function boo() {
        return new Boo(); 
    }
}

class Boo {
    function woo() {
        return "woo!";
    }
}

Template variables assigned

$foo = new Foo();
$tpl->assign('foo', $foo);

Template

{$foo->boo()->woo()}

Browser Output

woo! 
Jeff Standen
I was using Smarty 2, but it's good to know that Smarty 3 fixes it. I may update when it's got a final release.
Tim