For Programmers: Free Programming Magazines  


Home > Archive > PHP Smarty Templates > February 2005 > Re: [SMARTY] Re: calling {$foo->bar()->bar2()} ?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

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

2005-02-11, 4:02 pm

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:
>
>
>

Michal Frackowiak

2005-02-11, 4:02 pm

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.

best regards
michal

Jan Rosier wrote:[color=darkred]
> 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:
>
Boots

2005-02-11, 9:00 pm

--- 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
[color=darkred]
> best regards
> michal
>
> Jan Rosier wrote:
> should do
> that he
> sticks
> include it
> LOT), so
> in
> So
> recognize
> objects
> change
> (Smarty::_obj_call_regexp
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com