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

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


Joined: 04 Nov 2005
Posts: 3

PostPosted: Fri May 12, 2006 2:48 pm    Post subject: Static method calls Reply with quote

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
View user's profile Send private message
frayja
Smarty n00b


Joined: 04 Nov 2005
Posts: 3

PostPosted: Fri May 12, 2006 3:17 pm    Post subject: Code of the static method patch Reply with quote

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
View user's profile Send private message
lynweb
Smarty n00b


Joined: 18 Jan 2006
Posts: 4
Location: Norway

PostPosted: Thu Aug 31, 2006 1:05 pm    Post subject: Great! Reply with quote

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
View user's profile Send private message Visit poster's website
boots
Administrator


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

PostPosted: Thu Aug 31, 2006 3:23 pm    Post subject: Re: Great! Reply with quote

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
View user's profile Send private message
frugalprogrammer
Smarty n00b


Joined: 19 Sep 2008
Posts: 2

PostPosted: Fri Sep 19, 2008 6:25 pm    Post subject: Uhhh.... Reply with quote

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
View user's profile Send private message
ksnyde
Smarty Rookie


Joined: 19 Oct 2012
Posts: 12

PostPosted: Thu Dec 13, 2012 2:54 pm    Post subject: Reply with quote

Has this been addressed in later versions of Smarty? It does seem like a pretty obvious addition.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7276
Location: Lincoln Nebraska, USA

PostPosted: Thu Dec 13, 2012 2:57 pm    Post subject: Reply with quote

ksnyde wrote:
Has this been addressed in later versions of Smarty? It does seem like a pretty obvious addition.


have you tried it?

http://www.smarty.net/docs/en/advanced.features.static.classes.tpl
Back to top
View user's profile Send private message 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