Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

$this in modifiers

 
Post new topic   Reply to topic    Smarty Forum Index -> Feature Requests
View previous topic :: View next topic  

Is it a good idea to pass &$this to modifiers?
There is no need for this
15%
 15%  [ 2 ]
No Way
0%
 0%  [ 0 ]
No way!
0%
 0%  [ 0 ]
Maybe but not worth it
23%
 23%  [ 3 ]
No, not needed
0%
 0%  [ 0 ]
No, not needed
0%
 0%  [ 0 ]
It can't hurt
15%
 15%  [ 2 ]
Maybe
0%
 0%  [ 0 ]
Sure, why not (maybe)
0%
 0%  [ 0 ]
I like it
46%
 46%  [ 6 ]
Yes
0%
 0%  [ 0 ]
Total Votes : 13

Author Message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu May 01, 2003 1:13 pm    Post subject: $this in modifiers Reply with quote

EDIT: okay things are out of hand with the bb. sorry. Site Admin-- HELP!!!

EDIT: Sorry, I edited this post and it caused the poll to be deleted ?? I recreated the poll. The last results that I saw prior to this were:

1 No way
3 No, not needed
2 Maybe
1 Yes

Apologies. Please re-vote.

Hi!

I was wondering if it is a good idea to pass &$this to modifiers.

I know it goes against the idea of modifiers since it can create side-effects. On the other-hand, it gives modifiers pretty much the same power as custom functions but with a slightly simpler syntax in cases where there is only one parameter.

Here's just one example, a simple "var" modifer that emulates {assign}. The first parameter is the varname to assign to, the second paramter is either true (default) to have the modifier return output or false, for no output.

Code:
function smarty_modifier__var(&$smarty, $string, $var='', $output=True)
{
    if (empty($var)) {
        $smarty->trigger_error("smarty_modifier__var: missing $var");
    } else {
        $smarty->assign($var, $string);
    }
    if ($output)
        return $string;
}


For now, I've patched my smarty to pass &$this as the first parameter if the modifier starts with an underscore. Wink

So I can do:
Code:
{''Hello World!"|_var:"hello"}
-----
Hello World!


or
Code:
{''Hello World!"|_var:"hello":0}
{$hello}
-----
Hello World!


multi-assign:
Code:
{"Hello World!"|_var:"a"|_var:"b":0}
{$a} {$b}
-----
Hello World! Hello World!


variant multi-assign:
Code:
{"Hello"|_var:"less"|cat:" World!"|_var:"more":0}
{$less}<br/>{$more}
-----
Hello
Hello World!


use and assign:
Code:
{if $smarty.section.mysection.index|_var:"index" > 10}
    {$index}
{/if}


Quickly (dangerously) ignore a block by turning off output. None of the following block gets output, but the assign still occurs.
Code:
{if $smarty.section.mysection.index|_var:"index":0 > 10}
    {$index}
{/if}


Also handy for storing results from custom functions that don't support assign and from user defined objects.
Code:
{myfunction|_var:"myblock" file="..."}


There are many other possiblities besides assign. I know some won't like it, what I'm interested in knowing is the down-side, especially in terms of performance, compiling and cache wise.

Piped commands look neat. Smile

Wow. Thanks for reading all this and remember to vote!


Last edited by boots on Mon Jun 09, 2003 10:13 am; edited 4 times in total
Back to top
View user's profile Send private message
AZTEK
Smarty Pro


Joined: 16 Apr 2003
Posts: 235
Location: Purdue University

PostPosted: Thu May 01, 2003 2:32 pm    Post subject: Reply with quote

In case for some reason its needed it might be a good idea to pass it
_________________
"Imagine a school with children that can read and write, but with teachers who cannot, and you have a metaphor of the Information Age in which we live." -Peter Cochrane
Back to top
View user's profile Send private message Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Thu May 01, 2003 2:42 pm    Post subject: Reply with quote

some people think it's totally wrong to have $this inside a modifier, but i am not among them. Wink

it is hard to add $this transparently, since *all* parameters to a modifier already have meaning (consider sprintf).

with the cvs-version you can register an object's method as a modifier. this could of course be a method of your subclassed Smarty-class, or a method of an object, that knows a reference to the $smarty-object. maybe this is enough to get $this inside a modifier.

just MHO. Smile
Back to top
View user's profile Send private message Send e-mail Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu May 01, 2003 2:58 pm    Post subject: Reply with quote

messju wrote:
it is hard to add $this transparently, since *all* parameters to a modifier already have meaning (consider sprintf).


This is what I did in _run_mod_handlers():
Code:
$args = func_get_args();
// new code
if (substr($args[0],0,1) == "_") {
    $mod = array_shift($args);
    $done = array_shift($args);
    array_unshift($args, $mod, $done, &$this);
}

This hack forces all modifiers that begin with "_" to accept $this as the first parameter. I'm worried that its still sending a copy and not a reference, though.

messju wrote:
with the cvs-version you can register an object's method as a modifier. this could of course be a method of your subclassed Smarty-class, or a method of an object, that knows a reference to the $smarty-object. maybe this is enough to get $this inside a modifier.


What does that look like?

{'a'|myfunc->a:""} ??
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Thu May 01, 2003 3:12 pm    Post subject: Reply with quote

boots wrote:
What does that look like?

{'a'|myfunc->a:""} ??


no! Smile please no more method-calls inside templates.
the template doesn't know if a method or a function is called. it shouldn't care.

$smarty->register_modifier("foo", array(&$obj, "foo_m"));
(the array looks ugly or like a hack, but it's php's proposed way to pass a method-referece where a function-name would also be suitable (compare: http://www.php.net/manual/en/function.call-user-method.php ))

in the template it's simply
{"str"|foo:"param"}

but $obj->foo_m("param") gets called.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu May 01, 2003 3:27 pm    Post subject: Reply with quote

I see, so I'd register a $this aware modifier like:

class mod_smarty extends smarty
{
function mod_smarty()
{
$this->register_modifier("var", array(&$this, "_mod_assign"));
}
...
function _mod_assign($string, $var) { ... }
...
}

That is cool. But not plug-in friendly Smile. I like plugins because they self register. I can manage functionality by simply changing the plugins directory (poor-man's overloading).

Yeah, the array function passing looks funky, but I got used to it Wink

I see your point on not putting object method calls in template. Not saying I'm not going to do it though, but I'm going to do more to make sure that I don't have to.

Thanks for the tips!
Back to top
View user's profile Send private message
Roc
Smarty Rookie


Joined: 17 Apr 2003
Posts: 15

PostPosted: Fri May 02, 2003 10:57 pm    Post subject: Reply with quote

I had a need for this a few days ago. What I wanted to implement was the following:

Quote:

{$var|wrapper:"box.tpl"}


that should result in building a html wrapper around the value. As $this is not accessable within the modifier, there is no chance to do a $this->fetch().

I used a function instead to come around that - which works well, but is not as short as the modifier method:

Quote:

{wrapper file="box.tpl"}
{$var}
{/wrapper}
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Fri May 02, 2003 11:14 pm    Post subject: Reply with quote

@Roc:

I'm doing something very similar to what you describe (I laughed when I saw "wrap" I considered that name).

Does my post here http://www.phpinsider.com/smarty-forum/viewtopic.php?p=999&highlight=#999 ring any bells??

Although, your method of passing a template looks suspiciously like an include. That means that you must be mapping a tpl var to the included template. Are params to the mod also mapped?

The plugin that I'm working on allows you to inline this kind of work by changing the meaning of modifiers depending on the modifier context block that they are in.

I like modifiers so much that I also suggested a patch to the smarty-dev mailing list to allow optional spaces in modifiers. http://lists.php.net/article.php?group=php.smarty.dev&article=1332
Back to top
View user's profile Send private message
Roc
Smarty Rookie


Joined: 17 Apr 2003
Posts: 15

PostPosted: Sat May 03, 2003 12:04 am    Post subject: Reply with quote

You're right, in fact what I use is similar to an include. The params are also mapped. The reason I do it this way is to make it more comprehensive to the web designers I work together with. They are mostly not programmers and should not get confused by the need to use programming techniques. The term and behavior of a "wrapper" is more familiar to designers.
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sat May 03, 2003 12:18 am    Post subject: Reply with quote

Roc wrote:
The reason I do it this way is to make it more comprehensive to the web designers I work together with. They are mostly not programmers and should not get confused by the need to use programming techniques. The term and behavior of a "wrapper" is more familiar to designers.


I'm with you 100%. I think the Smarty provided verbs are just the start and more suited to programmers than content developers and designers. I'm all about creating custom verbage that is immediately accessible (excess-able?) by the target developers.

I'm glad to see others who are trying to do the same thing. {"Roc"|cat:"k"} on!
Back to top
View user's profile Send private message
ubaldo
Smarty Regular


Joined: 21 Apr 2003
Posts: 35
Location: Barcelona, Spain

PostPosted: Mon Jun 09, 2003 10:44 am    Post subject: Reply with quote

Another use of registering a method as a modifier is when writing a modifier that differs slightly from one of the standar modifiers. Let's say I want to write a modifier called my_date_format

Now, if in my_date_format you need to call smarty's standard date_format modifier, then you''ll need smarty pointer if you don't want to make a mess trying to second guess where the plugins are (see code)

require_once $this->_get_plugin_filepath('modifier', 'date_format');

I definitively could make use of a method as an modifier function.

So, is the proposed construct(see previous message):

$this->register_modifier("var", array(&$this, "_mod_assign"));

implemented?

Is it discarded?
Back to top
View user's profile Send private message Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Mon Jun 09, 2003 11:32 am    Post subject: Reply with quote

ubaldo wrote:
...
$this->register_modifier("var", array(&$this, "_mod_assign"));

implemented?
...


yep, implemented shortly after 2.5.0, that means it's available in cvs only for now.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
ubaldo
Smarty Regular


Joined: 21 Apr 2003
Posts: 35
Location: Barcelona, Spain

PostPosted: Mon Jun 09, 2003 12:25 pm    Post subject: Reply with quote

Thank you. Yes, I already noticed that it didn't work in 2.50. I'll give the CVS version a spin
Back to top
View user's profile Send private message Visit poster's website
ubaldo
Smarty Regular


Joined: 21 Apr 2003
Posts: 35
Location: Barcelona, Spain

PostPosted: Mon Jun 09, 2003 2:07 pm    Post subject: Reply with quote

Ok, I tried it with the following cvs versions (on top of 2.5 release tag)

1.378 smarty class
1.211 compiler class

and it seems to work fine. I'll do more testing before going to production. If you think I'm missing something or there is a potential pitfall, please, let me know.

Note: I'm using as a modifier callback a method on a smarty instance
(an instance of a Smarty class derivative, naturally)

Thank you
Back to top
View user's profile Send private message Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Mon Jun 09, 2003 11:41 pm    Post subject: Reply with quote

@ubaldo: no i see no pitfalls here. i consider the mehod-callback-stuff stable and i am pretty sure it will stay as is for the next release. using a method of a subclass of smarty as a callback is fully legal, also.

greetings
messju
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    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