Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Static method calls

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    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: 5611
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: 7368
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
Julian.Baker1
Smarty n00b


Joined: 15 Dec 2014
Posts: 4
Location: New York

PostPosted: Wed May 13, 2015 5:23 am    Post subject: Reply with quote

I think this is a good idea to incorporate in next versions...
Back to top
View user's profile Send private message Send e-mail Yahoo Messenger MSN Messenger
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed May 13, 2015 2:12 pm    Post subject: Reply with quote

What "this" and what is "good" in it?
Why ever you want to put code into templates?
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Wed May 13, 2015 7:10 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    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