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

New Modifier Plugin Type w/ $smarty Object

 
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
Rikaelus
Smarty Rookie


Joined: 25 Feb 2005
Posts: 14

PostPosted: Wed Jul 18, 2007 8:40 pm    Post subject: New Modifier Plugin Type w/ $smarty Object Reply with quote

In the past in my own system, and just now in IRC, I've found problems that would be more easily solved if the $smarty object were available within modifier-type plugins, in which its normally excluded. That combined with the fact that modifiers make up the only plugin type that doesn't receive the $smarty object, I'm driven to put forth the request for a new type that does.

To ensure backwards compatibility, changes in the plugin's file and function name would vary such that it would be detected as the new modifier type. Lets say "smodifier.name.php" / "smarty_smodifier_name()". When the function is called from a template, like a normal modifier (smodifiers would take precedence over normal modifiers, or vice-versa), Smarty would detect the kind of modifier it is and pass $smarty into it as the first variable accordingly.

Note I said it would be the first variable versus the last, like is normal. This would be the case since modifiers can receive an unspecified number of variables so the first spot would be the only guaranteed constant.

I've hacked my copy of Smarty already to provide the $smarty variable as the first to modifiers found in a hardcoded list, so I know that piece isn't too hard a change from what's there. What would take more digging is how to detect and store these new modifiers separate the existing type.

Usage Case:
I, and several other people I know, extend Smarty with their own class in which they can include additional presentation-logic concepts unique to their system. In my case I store localization instructions and a link to an instantiated copy of my system's translation class. The $smarty object found in the plugins is my extended class, so might have items within it that are valuable in a modifier capacity.

With the translation instructions and system available through $smarty in my modifiers, I'm able to handle localization in my templates as simple as lines like this:
{"TEXT001"|tr}

The "tr" modifier accesses the translation logic available through, lets say "$smarty->localizer->translate('TEXT001')" and returns the "modified" string.

It's much lighter-weight in templates than using blocks and easier to apply to {assign}, {if} and any other instances in which modifiers are usable.

Ultimately what this would do is open more doors for those of us that extend Smarty into realms in which access to $smarty becomes ever-more useful, or even required.
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Wed Jul 18, 2007 11:08 pm    Post subject: Reply with quote

not a perfect one-size-fits-all-solution, but:
you can register object-methods as modifiers. you can register your modifiers like
Code:
$smarty->register_modifer('tr', array(&$smarty, 'translate'))

on a |tr $smarty->translate() would be called then.
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 Jul 19, 2007 8:08 am    Post subject: Reply with quote

I think messju gives the best and most straight-forward solution for this. I myself have run against this limitation and have considered similar solutions to the one proposed. That said, I'd still recommend using registered methods on an extended Smarty class as modifiers until a more general solution is introduced (which is an if-and-when long-shot for now).

Best!
Back to top
View user's profile Send private message
Celeb
Administrator


Joined: 17 Apr 2007
Posts: 1025
Location: Vienna

PostPosted: Thu Jul 19, 2007 8:36 am    Post subject: Reply with quote

Another option could be making your Smarty object globally accessable by a Singleton or Service Locator (no global keyword, that's evil and not encapsulating Razz).
_________________
Darn computers always do what I tell them to instead of what I want them to do.
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