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

math modifier

 
Post new topic   Reply to topic    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
boots
Administrator


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

PostPosted: Sat May 03, 2003 12:16 pm    Post subject: math modifier Reply with quote

Hi all.

Math in Smarty seems to be the hot topic. Until the engine natively supports it, math is going to remain hard. I always thought the math plugin was wonky. Just having to type "expression" all the time made it a pass for me.

I've noticed a few solutions here to tackle small math problems (like add and increment, etc).

I like the idea of a modifier instead of a function plugin since it allows you to inline your math and also eliminate many un-needed assigns.

Here's mine, for what its worth. It does not support operators like ++ and -- but otherwise supports everything that PHP does via PHP's eval Rolling Eyes. That means think twice before using it!

Without native math support you are either going to have to do an eval or you are going to have to do some token parsing and replacement. That's the way the math function plugin works and this modifier can be changed accordingly if you are of that ilk. I figure eval can't be that much worse than a PHP parsing algorithm.

You can do:
Code:
{"1+2"|math}
{"(sin($theta)-1)/cos($theta))"|math}

and a whole lot more.

Remember that Smarty will eval all vars before the string is passed to the math modifier.

The code:

[php:1:3d7ab64931]function smarty_modifier_math($string)
{
eval ('$result='.$string.';');
return $result;
}[/php:1:3d7ab64931]

EDIT: You may want to call this calc instead of math since it is more like a calculator than a generic expression evaluator.


Last edited by boots on Sat May 24, 2003 7:02 am; edited 2 times in total
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Fri May 09, 2003 11:00 pm    Post subject: Reply with quote

Please see http://www.phpinsider.com/smarty-forum/viewtopic.php?t=225 for a much faster compiler plugin alternative.
Back to top
View user's profile Send private message
Wom.bat
Smarty Pro


Joined: 24 Apr 2003
Posts: 107
Location: Munich, Germany

PostPosted: Fri May 09, 2003 11:15 pm    Post subject: Reply with quote

it will hopefully supported by the engine, soon
I am working with a colleague (he's doing the main coding job *g*) on a Smarty patch that brings solid math support for Smarty Wink
messju has the patch, it seems to work quite fine now, we are currently looking for bugs Wink

things like

{$foo+$bar.test->test.t%$baz|round:1*134232+10.100+$b+10}

are already working perfectly Wink

stay tuned Smile

David
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Fri May 09, 2003 11:43 pm    Post subject: Reply with quote

Sounds great, but as I mention in the other thread, I wonder if adding the additional parsing to the main core is really that much of an overall improvement in comparison to using the compiler plugin. Why slow down the entire parsing engine for something that is needed only infrequently and can easily be done using a compiler function which already shows great promise and speed.

Just my opinion, though.

Aside: I would prefer that modifier parameters where anything other than simple vars (ie. expressions) be enclosed in quotes.


Last edited by boots on Fri May 09, 2003 11:50 pm; edited 1 time in total
Back to top
View user's profile Send private message
Wom.bat
Smarty Pro


Joined: 24 Apr 2003
Posts: 107
Location: Munich, Germany

PostPosted: Fri May 09, 2003 11:47 pm    Post subject: Reply with quote

it's not in the main core... it's in the compiler...

I have no clue if it will make it into the next release, as I don't make this decision Smile messju seemed quite happy with it, so let's hope for the best Wink
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Fri May 09, 2003 11:49 pm    Post subject: Reply with quote

It will still slow down the compiler, which on the whole is not the worst thing, but should be avoided where possible. Whatever. I trust the people making the decisions--they haven't let us down yet!!
Back to top
View user's profile Send private message
Wom.bat
Smarty Pro


Joined: 24 Apr 2003
Posts: 107
Location: Munich, Germany

PostPosted: Fri May 09, 2003 11:55 pm    Post subject: Reply with quote

"make simple math easier" has been on the TODO list for aeons...
and the small patch which brought simple math a few days ago didn't even support $foo+$bar, or $foo+1*2...
the performance impact is quite minimal as far as I can judge on that at the moment, and some features are definitely worth 2ms more compiling time Wink

we'll see what's to come, I trust 'em, too, it'll be the right decision Smile
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Sat May 10, 2003 12:07 am    Post subject: Reply with quote

David: Don't get me wrong--I want fast math! Just not at the expense of non-math operations. Many sites don't use any math at all--are they being served by even a minimal slowing down of the compilation engine?

In the other thread, messju hints (but doesn't actually say) that all math should be done OUT of template. Hee hee. I can't agree with that, but he does raise a good point--there is already too much rope in Smarty that people use to hang themselves with.

It comes down to my original point--the core should be as sparse as possible and additional functionality should be provided by plug-ins on an as-needed basis. When I say core, that includes the compilation and display phases.

Anyhoo, one thing I love about Smarty is the breadth and depth of choices that it offers. If nothing else, I hope that the patch you mention makes it to the CVS since I'd like to try it out for myself Wink
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Plugins 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