Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Problem with $foo->getbar->getbaz

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Feature Requests
View previous topic :: View next topic  
Author Message
charles.bourasseau
Smarty Rookie


Joined: 07 Aug 2008
Posts: 5

PostPosted: Thu Aug 07, 2008 2:24 pm    Post subject: Problem with $foo->getbar->getbaz Reply with quote

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
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Thu Aug 07, 2008 9:26 pm    Post subject: Reply with quote

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
View user's profile Send private message
charles.bourasseau
Smarty Rookie


Joined: 07 Aug 2008
Posts: 5

PostPosted: Thu Aug 07, 2008 11:17 pm    Post subject: Reply with quote

Thanks for your answer Smile

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
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Fri Aug 08, 2008 1:19 am    Post subject: Reply with quote

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
View user's profile Send private message
charles.bourasseau
Smarty Rookie


Joined: 07 Aug 2008
Posts: 5

PostPosted: Fri Aug 08, 2008 7:36 pm    Post subject: Reply with quote

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
View user's profile Send private message
Celeb
Administrator


Joined: 17 Apr 2007
Posts: 1025
Location: Vienna

PostPosted: Mon Aug 11, 2008 2:27 pm    Post subject: Reply with quote

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
View user's profile Send private message
charles.bourasseau
Smarty Rookie


Joined: 07 Aug 2008
Posts: 5

PostPosted: Tue Aug 12, 2008 12:41 pm    Post subject: Reply with quote

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
View user's profile Send private message
charles.bourasseau
Smarty Rookie


Joined: 07 Aug 2008
Posts: 5

PostPosted: Tue Aug 12, 2008 6:37 pm    Post subject: Reply with quote

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
View user's profile Send private message
Celeb
Administrator


Joined: 17 Apr 2007
Posts: 1025
Location: Vienna

PostPosted: Wed Aug 13, 2008 8:51 am    Post subject: Reply with quote

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
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Wed Aug 13, 2008 3:54 pm    Post subject: Reply with quote

I think if you use the {assign} tag to assign an object to a variable the variable will be an object as well. However there is a difference how you will access a registered object and assigned objects in your template. Also parameter passing is different. See http://www.smarty.net/manual/en/advanced.features.php#advanced.features.objects
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Feature Requests All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP