View previous topic :: View next topic |
Author |
Message |
charles.bourasseau Smarty Rookie
Joined: 07 Aug 2008 Posts: 5
|
Posted: Thu Aug 07, 2008 2:24 pm Post subject: Problem with $foo->getbar->getbaz |
|
|
Hi,
I have a probleme, I want to chain method in the Template but it seems to doesn't work. I would like to do :
{$bookObject->getPart->getChapter->getTitle}
I found this topic : Refering to nested object methods (sorry i'm new I can't post the url you can make a research) and the answer from Boots Quote: | Right. With PHP5, it just needs the compiler to observe the syntax in the template and to emit the corresponding PHP5 code. If you've done the search you probably know I don't favour this very much. It is really a programmer's idiom and most people seem to want it to chain get methods on nested objects. This can easily be done under PHP5 using __get(). This means you can do things like $foo->bar->baz in template instead of $foo->getbar()->getbaz() etc. The tempation to pass in "pure" application objects into your templates is greater with method derefrencing and IMHO, that is a very slippery slope. |
But I don't understand how the fonction __get() works. Maybe it exists an other way to chain method... I don't know, I'm lost... |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Thu Aug 07, 2008 9:26 pm Post subject: |
|
|
Samrty's syntax does not allow methode chaining because this is not supported by PHP 4 to which Smarty is compatiple.
If you have to do it on template level there is just this workaround:
Code: | {assign var=foo value=$bookObject->getPart}
{assign var=foo2 value=$foo->getChapter}
{$foo2->getTitle}
|
|
|
Back to top |
|
charles.bourasseau Smarty Rookie
Joined: 07 Aug 2008 Posts: 5
|
Posted: Thu Aug 07, 2008 11:17 pm Post subject: |
|
|
Thanks for your answer
and about this way, it can work with smarty 2.6.18 ?
http:// codepoets.co.uk/smarty_chained_function_calls
I will try tomorrow your method, but I have already try almost the same so I am not sure ... |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Fri Aug 08, 2008 1:19 am Post subject: |
|
|
Please note that the patch provided at codepoets will work only with php5. The workarouend posted by me should work with all PHP versions. |
|
Back to top |
|
charles.bourasseau Smarty Rookie
Joined: 07 Aug 2008 Posts: 5
|
Posted: Fri Aug 08, 2008 7:36 pm Post subject: |
|
|
unfortunately it doesn't work again. I do like that :
Code: |
{assign var=foo value=$bookObject->getBookInfo}
{$foo2->getTitle}
|
There is not error from syntax check but it doesn't display the Title. I succeeded with this way :
Code: |
{bookObject->getBookInfo assign="theBookInfo"}
{$theBookInfo->getTitle()}
|
This way works but I can't do like that because I need to put a argument in the method and I don't know which.
I really need a way like that: Code: | {$bookObject->getBookInfo()->getPart('with argument')->getChapter('with argument')->getTitle} |
and I would like that the title is displayed...
Help me it's really difficult ! |
|
Back to top |
|
Celeb Administrator
Joined: 17 Apr 2007 Posts: 1025 Location: Vienna
|
Posted: Mon Aug 11, 2008 2:27 pm Post subject: |
|
|
Regarding your first example: foo != foo2 _________________ Darn computers always do what I tell them to instead of what I want them to do. |
|
Back to top |
|
charles.bourasseau Smarty Rookie
Joined: 07 Aug 2008 Posts: 5
|
Posted: Tue Aug 12, 2008 12:41 pm Post subject: |
|
|
it's just a typo, the solution was that I put a lot of new method in the class Book but it's not a good way because these method are only used by smarty and normally I can do without... |
|
Back to top |
|
charles.bourasseau Smarty Rookie
Joined: 07 Aug 2008 Posts: 5
|
Posted: Tue Aug 12, 2008 6:37 pm Post subject: |
|
|
is it possible to register an object in the Template ?
If we put assign is it just a variable and not really an object ? |
|
Back to top |
|
Celeb Administrator
Joined: 17 Apr 2007 Posts: 1025 Location: Vienna
|
Posted: Wed Aug 13, 2008 8:51 am Post subject: |
|
|
I guess you could register objects in custom template functions. _________________ Darn computers always do what I tell them to instead of what I want them to do. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
|
Back to top |
|
|