|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
Lemon Juice Smarty Pro
Joined: 24 May 2006 Posts: 109
|
Posted: Thu Apr 19, 2007 5:18 am Post subject: How to access $smarty object in a modifier? |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Apr 19, 2007 5:42 am Post subject: |
|
|
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 |
|
Lemon Juice Smarty Pro
Joined: 24 May 2006 Posts: 109
|
Posted: Sun Apr 22, 2007 7:49 am Post subject: |
|
|
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 |
|
|
|
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
|