Re: [SMARTY] Re: calling {$foo->bar()->bar2()} ?

From: Date: Fri, 11 Feb 2005 20:12:39 +0000
Subject: Re: [SMARTY] Re: calling {$foo->bar()->bar2()} ?
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-12240@lists.php.net to get a copy of this message
--- Michal Frackowiak <michalf@ncac.torun.pl> wrote: > Thanx - yes, it should do the trick. But if you want to use e.g.: > > $link->setParameter("foo", 3)->setParameter("foo2", > $bar)->setSecure()->setTemplate("SomeTemplate")->render() > > there is not much point in doing it through {assign...} ;-) > > Anyway - I shall try to modify Smarty a bit to include method > dereferencing. Although this will not work with PHP4, PHP5 will be a > standard soon. The object model is IMHO great and allowes to build a > really nice framework. well, I remembered to reply to all this time :) I figured you were trying to access a deep getter. I get the impression that more and more folks are passing in their actual application objects into the template -- probably as a matter of convenience. To me, the general case should be that if an object has getter/setter methods it has no business being passed to Smarty. Why? Because there is no need for such things in a template where property access should suffice (and arrays are better yet). Especially if you are exposing objects with setters -- you almost certainly don't want your templates to be able to modify the datamodel. Here are some suggestions: - Create a function (or even a method on the object) that collapses your objects into either an array or simple properties on a StdClass object. However, avoid the tempation of straight collapse -- while it is advantadgeous not to have deeply nested structures in your application, it is even more true for your template data. In other words, you may want to flatten your interface somewhat. Collapsing to arrays is nice because your designers can use the standard dot notation for access. - If you are content with passing your app objects into Smarty then consider implementinh a __get method on those objects that rely on getters. Your __get method can automatically call the getter for the requested parameter and return that value. IIR, $foo->bar->baz is supported but I could be wrong... My main point is that unless you really know what you are doing and don't have any other choice, you really should be passing pure processed data into the templates rather than objects which can be used to retrieve data. Now I expect I am starting to get on the nerves of the PHP5 faithful but I truly believe that just because new features are available in the language it doesn't imply they are needed in every situation. Remember, we're trying to shield the template users from the complexity of the application design. greetings, xo boots > best regards > michal > > Jan Rosier wrote: > > Maybe this helps, not as pretty as {$foo->bar()->bar2()}, but > should do > > the trick > > > > {assign var="bar" value=$foo->bar()} > > {$bar->bar2()} > > > > > > Greetings, > > Jan Rosier > > > > * When asked how World War III would be fought, Einstein replied > that he > > didn't know. But he knew how World War IV would be fought: With > sticks > > and stones. * > > > > > > Michal Frackowiak wrote: > > > >> Ok - now I know (thanx xo boots) - method dereferencing is not > >> supported in PHP4 and therefore there was not much point to > include it > >> in Smarty (yet). > >> > >> However I think it would be a nice feature (well, I need it a > LOT), so > >> the question: "HOW TO DO THIS" remains. I would try to hack Smarty > in > >> some free time, but if there is any nice idea how to do this - it > >> would be great! > >> > >> all best > >> > >> michal > >> > >> > >> > >> Michal Frackowiak wrote: > >> > >>> Hi! > >>> > >>> I am starting to use Smarty in order to re-write my web portal. > So > >>> far Smarty is the answer for my problems. > >>> > >>> Hovewer I was surprised that the Smarty compiler does not > recognize > >>> the structure > >>> > >>> $foo->bar()->bar2() > >>> > >>> i.e. $foo->bar() returns object $foo2 and one wants to call the > >>> method bar2() on this resulting $foo2. > >>> > >>> According to the comments in the source code > >>> (Smarty_Compiler.class.php) following structures when calling > objects > >>> are supported: > >>> > >>> $foo->bar > >>> $foo->bar() > >>> $foo->bar("text") > >>> $foo->bar($foo, $bar, "text") > >>> $foo->bar($foo, "foo") > >>> $foo->bar->foo() > >>> $foo->bar->foo->bar() > >>> $foo->bar($foo->bar) > >>> $foo->bar($foo->bar()) > >>> $foo->bar($foo->bar($blah,$foo,44,"foo",$foo[0].bar)) > >>> > >>> I know very little about regexp and do not feel like able to > change > >>> the Smarty engine. > >>> > >>> Would the modification involve only regexps > (Smarty::_obj_call_regexp > >>> and others) or compiler core as well? > >>> > >>> I think such modification would be great!!! > >>> > >>> best regards > >>> > >>> Michal

« previous php.smarty.general (#12240) next »