Re[2]: Passing in objects by reference (Pull approach w/ Smarty)
| From: | Oleg Rekutin | Date: | Thu, 05 Jul 2001 22:44:40 +0000 |
| Subject: | Re[2]: Passing in objects by reference (Pull approach w/ Smarty) | ||
| References: | 1 2 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-1120@lists.php.net to get a copy of this message | ||
Hello Andrei,
On Thursday, July 05, 2001, 2:27:04 PM, in message "[smarty] Passing
in objects by reference (Pull approach w/ Smarty)", Andrei wrote:
AZ> On Thu, 05 Jul 2001, Oleg Rekutin wrote:
>> So here's my question. Is it possible to pass in objects to Smarty
>> templates by reference? Something like assign("security",
>> $context->get('security'));, where all method calls to security within
>> the Smarty templates are performed on the reference returned by
>> Context::get. This way, only one copy of the security object is
>> maintained, and thus only one state is updated.
>>
>> For instance, all screen templates do $context->set('title', 'Log
>> in') and
>> then the layout template displays $context->get('title') in the
>> <title></title> tags. This requires that the screen template modifies
>> one and only one context object, thru a reference, so that the layout
>> template can retrieve the set value from the same object.
>>
>> However, as far as I see, assign() takes in the value by parameter and
>> not reference. Is there any way to have some sort of "assign_by_ref"?
AZ> It would be possible to have assign take the value by reference.
AZ> However, how would this be useful to you, since Smarty doesn't allow
AZ> calling object methods in templates?
D'oh! I had the impression that Smarty allowed calling object methods--I
don't know from where, I thought that I spotted it in the ChangeLog or
something. I apologize for not verifying my impression.
Would it then be possible for Smarty to support calling object
methods?
I looked at the "Componentized Templates" section in the
documentation, and the example there looks like an attempt at the pull
model. However, it couples logic with the template, which may be fine
if there's only one such shared template across the site, but what if
there are five different templates that display ticker name and price
in five different ways? Or maybe one of the five templates accesses
the ticker price to earnings ratio, or some other information.
If the ticker retrieval logic is moved out into one shared class, then
unlimited templates can take advantage of this.
With the {php} section, the example would then change to:
{php}
$tickers->fetch("YHOO", $ticker_name, $ticker_price);
// assign template variables
$this->assign("ticker_name",$ticker_name);
$this->assign("ticker_price",$ticker_price);
{/php}
Stock Name: {$ticker_name} Stock Price: {$ticker_price}
(Well, it doesn't have to be $tickers->fetch, it can be a simple API
using fetch_ticker.) But wouldn't it be nicer if it were simply
something like:
{$tickers->fetch("YHOO")}
Stock Name: {$ticker_name} Stock Price: {$ticker_price}
(Tickers::fetch, in this case, assigns template variables using the
Smarty API).
Dunno, just throwing some ideas out, I guess since I'm not going to
get around to implementing Smarty into the current project and since
I've no time to contribute object method calling support to Smarty,
I'll just shut up :)
- Oleg