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

How to access $smarty object in a modifier?

 
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 -> General
View previous topic :: View next topic  
Author Message
Lemon Juice
Smarty Pro


Joined: 24 May 2006
Posts: 109

PostPosted: Thu Apr 19, 2007 5:18 am    Post subject: How to access $smarty object in a modifier? Reply with quote

I want to access the $smarty object in a modifier but cannot find any elegant solution apart from from using "global". I have created a modifier which formats price for display ("format_price") and the formatting is done differently depending on the language. The language is set by my application like this:
Code:
$smarty->assign('lang', $lang);

Now I can format prices in my tpls like this:
Code:
{$price|format_price:$lang}

But I don't want to type $lang every time, it would be better if I could just write:
Code:
{$price|format_price}

But then my modifier has to access $lang - either by fetching the value from the smarty object or accessing $lang directly - but in both cases I would have to use $GLOBALS - is there any more elegant way to do this?
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Thu Apr 19, 2007 5:42 am    Post subject: Reply with quote

Hi.

The easy way is to make your modifier a method of your custom Smarty subclass; of course, manually register your modifier in the constructor. As a method of the Smarty object, your modifier can then access $this -- which is, of course, the Smarty instance. You lose the lazy loading, but that's not really a big deal in this case.

HTH
Back to top
View user's profile Send private message
Lemon Juice
Smarty Pro


Joined: 24 May 2006
Posts: 109

PostPosted: Sun Apr 22, 2007 7:49 am    Post subject: Reply with quote

Thanks, it works very well. However, I had to move my modifiers from their separate files into the smarty subclass. Also, I believe that speedwise the dirty way of using $GLOBALS['lang'] must be a bit better - but that's the trade-off for using objects...
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 -> General 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