View previous topic :: View next topic |
Author |
Message |
frayja Smarty n00b
Joined: 04 Nov 2005 Posts: 3
|
Posted: Fri May 12, 2006 2:48 pm Post subject: Static method calls |
|
|
Hello fello-smarty-users,
I would like to be able to make static method calls. After some browsing on the form I found a few posts about this. One says that Smarty does not suppost this, and another says that it could be done with a modifier...
Using a modifier would (in my opinion) be nasty.
So I would like to have it done by the smarty compiler... but why wasn't this included in the first place? Is there some reason for this?
Just to be on the safe side, here is an example:
{foreach from=MyClass::getArray() item="data"}
{$data}
{/foreach}
Currently 'MyClass::getArray()' is translated into a string. This is done in the method Smarty_Compiler::_parse_attrs().
I have already made a patch for myself, but it would be much better if Smarty could do it on it's own... or perhaps I made a big mistake in wanting to call static methods in the first place?
Regards,
Frayja _________________ You can tell whether a man is clever by his answers.
You can tell whether a man is wise by his questions.
- Naguib Mahfouz (Nobel Prize Winner) |
|
Back to top |
|
frayja Smarty n00b
Joined: 04 Nov 2005 Posts: 3
|
Posted: Fri May 12, 2006 3:17 pm Post subject: Code of the static method patch |
|
|
To use this patch, you have to make a subclass of the smarty compiler... this is already described in the smarty documentation...
This is the method patch...
Code: |
function _parse_attrs( $tag_args )
{
$attrs = parent::_parse_attrs( $tag_args );
foreach( $attrs as $key=>$value )
{
// perhaps this was intended as a static callback?
if( preg_match( '#^["\']([a-zA-Z_]\w*::[a-zA-Z_]\w*)\((.*)?\)["\']$#', $value, $matches ) )
{
$arguments = '()';
if( isset( $matches[2] ) )
{
// strip '".' and '."' from beginning and end
$arguments = substr( $matches[2], 2, -2 );
// remove '.",".' from between parameters
$arguments = explode( '.",".', $arguments );
// combine arguments into string
$arguments = '('.implode( ',', $arguments ).')';
}
$attrs[$key] = $matches[1].$arguments;
}
}
return $attrs;
}
|
_________________ You can tell whether a man is clever by his answers.
You can tell whether a man is wise by his questions.
- Naguib Mahfouz (Nobel Prize Winner) |
|
Back to top |
|
lynweb Smarty n00b
Joined: 18 Jan 2006 Posts: 4 Location: Norway
|
Posted: Thu Aug 31, 2006 1:05 pm Post subject: Great! |
|
|
I agree, this should be included in the Smarty Compiler by default imho.
Though, that's the great thing about smarty since it's OOP-code, it's easy to extend it. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Aug 31, 2006 3:23 pm Post subject: Re: Great! |
|
|
lynweb wrote: | I agree, this should be included in the Smarty Compiler by default imho. |
Why? Since when does a a template need an arcane construct like a static call? Why should a template user have to differentiate between static calls, normal modifiers, plugin functions, assigned objects, etc? Having too many options just so that we can mimic every possible thing that PHP does is not an ideal solution in my mind. |
|
Back to top |
|
frugalprogrammer Smarty n00b
Joined: 19 Sep 2008 Posts: 2
|
Posted: Fri Sep 19, 2008 6:25 pm Post subject: Uhhh.... |
|
|
Because good design means that you have reusable code in a static method call? This is just good sense.
For example, say I have an overriden Datetime class for some special reporting options. I put those options in a static call in my new Datetime class so I can reuse them in all my tpls.
It's code reuse, it's good practice, and I'm honestly surprised that Smarty would be so silly as to not include a simple pattern such as this in your codebase. |
|
Back to top |
|
ksnyde Smarty Rookie
Joined: 19 Oct 2012 Posts: 12
|
Posted: Thu Dec 13, 2012 2:54 pm Post subject: |
|
|
Has this been addressed in later versions of Smarty? It does seem like a pretty obvious addition. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
|
Back to top |
|
Julian.Baker1 Smarty n00b
Joined: 15 Dec 2014 Posts: 4 Location: New York
|
Posted: Wed May 13, 2015 5:23 am Post subject: |
|
|
I think this is a good idea to incorporate in next versions... |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Wed May 13, 2015 2:12 pm Post subject: |
|
|
What "this" and what is "good" in it?
Why ever you want to put code into templates? |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Wed May 13, 2015 7:10 pm Post subject: |
|
|
Julian.Baker1 wrote: | I think this is a good idea to incorporate in next versions... |
Why are you replying on outdated issues.
Anyway it's already implemented |
|
Back to top |
|
|