View previous topic :: View next topic |
Author |
Message |
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sat May 03, 2003 12:16 pm Post subject: math modifier |
|
|
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 . 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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
|
Back to top |
|
Wom.bat Smarty Pro
Joined: 24 Apr 2003 Posts: 107 Location: Munich, Germany
|
Posted: Fri May 09, 2003 11:15 pm Post subject: |
|
|
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
messju has the patch, it seems to work quite fine now, we are currently looking for bugs
things like
{$foo+$bar.test->test.t%$baz|round:1*134232+10.100+$b+10}
are already working perfectly
stay tuned
David |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Fri May 09, 2003 11:43 pm Post subject: |
|
|
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 |
|
Wom.bat Smarty Pro
Joined: 24 Apr 2003 Posts: 107 Location: Munich, Germany
|
Posted: Fri May 09, 2003 11:47 pm Post subject: |
|
|
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 messju seemed quite happy with it, so let's hope for the best |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Fri May 09, 2003 11:49 pm Post subject: |
|
|
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 |
|
Wom.bat Smarty Pro
Joined: 24 Apr 2003 Posts: 107 Location: Munich, Germany
|
Posted: Fri May 09, 2003 11:55 pm Post subject: |
|
|
"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
we'll see what's to come, I trust 'em, too, it'll be the right decision |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sat May 10, 2003 12:07 am Post subject: |
|
|
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 |
|
Back to top |
|
|