View previous topic :: View next topic |
Author |
Message |
DiLDoG Smarty Rookie
Joined: 24 Aug 2003 Posts: 5
|
Posted: Mon Oct 20, 2003 12:45 pm Post subject: Math doesnt work? |
|
|
{assign var="votecount" value="$image.posvotes + $image.negvotes"}
results in
"Array.posvotes + Array.negvotes"
and have lots of other problems with math :/
{$foo * 10} doesnt work as well
(Smarty260RC2) |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Mon Oct 20, 2003 12:53 pm Post subject: |
|
|
- remove the "" around the attribute-value
(... value=$image.posvotes+$image.negvotes ...)
- don't use spaces with math:
( {$foo*10} ) |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Mon Oct 20, 2003 5:05 pm Post subject: |
|
|
just for the records. the code above works like i said. but you pointed me to a problem with backticks.
{$image.posvotes+$image.negvotes} works, but
{"`$image.posvotes+$image.negvotes`"} (which should be merely the same) didn't.
i committed a fix, so this it works in CVS now and will be so in RC3 |
|
Back to top |
|
Troublegum Smarty Rookie
Joined: 07 Sep 2003 Posts: 33 Location: Germany
|
Posted: Mon Oct 20, 2003 5:45 pm Post subject: |
|
|
messju wrote: | - don't use spaces with math:
( {$foo*10} ) |
In {if} statements, all operators must have a leading and trailing space. why not also in maths? I am afraid the syntax is not very consequent. |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Mon Oct 20, 2003 6:28 pm Post subject: |
|
|
no it isn't, unfortunately. we have {if}, {math} and simple-math. all are parsed and executed or compiled on their own little engines. i think we'll have to cope with that for now.
but at least in {if} spaces are a not mandatory in all places AFAIK. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Mon Oct 20, 2003 9:13 pm Post subject: |
|
|
messju:
can we use two sets of `` in a single expression?
{"`$a` and `$b`} ?
Answered on IRC: YES |
|
Back to top |
|
andre Smarty Pro
Joined: 23 Apr 2003 Posts: 164 Location: Karlsruhe, Germany
|
Posted: Wed Oct 22, 2003 10:54 am Post subject: |
|
|
Math within backticks is cool... but how about modifiers. Sometimes I would with to do something like this:
{foo bar="Mail to `$email|lower`"}
I know this could lead to massive problems (like {foo `bar="blah|blub:\"xx\"|xyz`"} but it would be cool also |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Oct 22, 2003 3:37 pm Post subject: |
|
|
andre: tee hee--you should have been on IRC To be honest, I was shocked that simple math was allowed in backticks (since the manual says they are for vars, not expressions). Anyways, I don't think it would be that cool. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Oct 22, 2003 4:50 pm Post subject: |
|
|
messju wrote: | just for the records. the code above works like i said. but you pointed me to a problem with backticks.
{$image.posvotes+$image.negvotes} works, but
{"`$image.posvotes+$image.negvotes`"} (which should be merely the same) didn't.
i committed a fix, so this it works in CVS now and will be so in RC3 |
This may open a can of worms. Thus far, the backticks have been used to escape a single variable within quotes, nothing more. IMHO, supplying more and more complex syntax only encourages bad template coding practices.
Rule of thumb, if it seems too difficult to figure out, it probably is. If the template starts looking too perplexing, it probably is. Figure out a way to let PHP do more of the work, and/or simplify your assigned variables. Keep those templates clean and simple.
As for adding math operators to backticks (and modifiers, etc.), I'm hesitant. I think there should be a better way to approach the problem other than continually extending the template syntax support. Feel free to argue though |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Wed Oct 22, 2003 5:14 pm Post subject: |
|
|
mohrt wrote: | This may open a can of worms. Thus far, the backticks have been used to escape a single variable within quotes, nothing more. IMHO, supplying more and more complex syntax only encourages bad template coding practices. |
IMHO the can of worms was opened when simple-math was added.
i neither added the math to the backticks, nor do i like the fact that they are accepted here. i just tried to make the generated code be a little more reasonable.
feel free to back this out of Smarty_Compiler::_parse_var() . i'd really appreciate that.
[Edit: to be clear: i meant back out the fact that simple-math is available inside backticks here. i didn't mean back out simple math at all] |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Oct 22, 2003 5:21 pm Post subject: |
|
|
messju wrote: | mohrt wrote: | This may open a can of worms. Thus far, the backticks have been used to escape a single variable within quotes, nothing more. IMHO, supplying more and more complex syntax only encourages bad template coding practices. |
IMHO the can of worms was opened when simple-math was added.
i neither added the math to the backticks, nor do i like the fact that they are accepted here. i just tried to make the generated code be a little more reasonable.
feel free to back this out of Smarty_Compiler::_parse_var() . i'd really appreciate that. |
I agree, simple math probably isn't necessary. It seemed harmless enough at the time. Simple math was added to 2.6.0-RC1, meaning that it could safely be backed out before the 2.6.0 release. I have no objections with removing it altogether. Thoughts? |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Oct 22, 2003 10:02 pm Post subject: |
|
|
mohrt wrote: | I agree, simple math probably isn't necessary. It seemed harmless enough at the time. Simple math was added to 2.6.0-RC1, meaning that it could safely be backed out before the 2.6.0 release. I have no objections with removing it altogether. Thoughts? |
I think that's a bit of the baby/bathwater problem. I concur that the original intent for backticks (simple var retrieval) should be respected. Still, simple math is a good idea and support for it should be somewhere. I also agree that more syntax (particularly to do the same thing that other syntax does) is not terribly useful. Certainly, extra back-tick features are not needed, IMHO.
There is a maintenence issue with simple-math and it does limit certain future options, but I think that it meets a need/issue that many users have raised. I propose that a differentiation be made between a variable and an expression (ie. simple-math) by insisting that expressions are enclosed in parens (). This would change the simple math so that {$a+1} becomes {($a+1)}. Among other things, it would make it simpler to disallow simple math in backticks, I think. |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Thu Oct 23, 2003 8:37 am Post subject: |
|
|
maybe.
first of all: i didn't want to encourage backing out simple-math fully but edited my post too late.
when i committed the backticks-fix, i didn't realise the whole picture and any language-syntax-issues. i just saw the thing trough the peep-hole of the compiler's generated code.
i saw
leads to
Code: | "".$this->_tpl_vars['a']+$this->_tpl_vars['b']."" |
i saw this is wrong because the "." binds left-associative with the same precedence as the "+" does. and i made it lead to:
Code: | "".($this->_tpl_vars['a']+$this->_tpl_vars['b'])."" |
which fixed it sufficiently to me.
if i had raised this issue with an evil grin just with the intention to get simple-math backed out, i would consider myself really clever. but that wasn't the fact.
i don't know if there is a "maintenaince issue" as boots called it. i just told him that i'm not capable of maintaining (read fixing) _parse_var() at the moment, because i didn't have time to get a full grasp of the simple-math-code and that i'm likely to break things if i try to fix something in there. so i don't feel too well with simple-math right now, but that shouldn't be a reason to get rid of it fully.
i fully agree that math doesn't belong into backticks and that even less modifiers would belog there.
first of all we should disencourage using math inside of backticks, regardless if the compiler accepts it or not. people must know that their templates are doomed to break in the future if they rely on this "feature".
i'm sure we are to encounter more unforseen side-effects of simple math if we keep it, but i also know that there is useful application for it. so i'm "-0" for simple-math right now |
|
Back to top |
|
andre Smarty Pro
Joined: 23 Apr 2003 Posts: 164 Location: Karlsruhe, Germany
|
Posted: Thu Oct 23, 2003 11:15 am Post subject: |
|
|
Simple math is very useful for displaying things like {$filesize/1024|number_format:2} but I am sure there are some people out there who create very complex structures with it If I remember correctly simple math was a heavily requested feature before 2.6.0 RC-1. So removing it will destroy the work of some people using it already (like myself).
Perhaps you could add simple math to the $security_settings array as "ALLOW_SIMPLEMATH" or add another member variable to Smarty class. This way you could turn off it by default. But I don't see any advantage of using {math assign="filesize_formatted" equation="x/1024" x=$filesize}{$filesize_formatted|number_format:2} instead of the simpler and more readable code above.
Therefore removing simple math is not a way to force the users to write clean/readable/easy to understand templates in my eyes.
Regarding my previous posting: I wouldn't vote for adding modifiers to backticks. Otherwise code will get more unreadable instead of simpler. And I'm using Smarty because it will make work simpler and more easy to understand for others |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Oct 23, 2003 2:34 pm Post subject: |
|
|
boots wrote: | There is a maintenence issue with simple-math and it does limit certain future options, but I think that it meets a need/issue that many users have raised. I propose that a differentiation be made between a variable and an expression (ie. simple-math) by insisting that expressions are enclosed in parens (). This would change the simple math so that {$a+1} becomes {($a+1)}. Among other things, it would make it simpler to disallow simple math in backticks, I think. |
I like the parenthesis idea, I think boots even brought that up once before. So lets look at how this might work:
{($foo+1)}
{($foo*$bar)}
{($foo->bar-$bar[1]*$baz->foo->bar()-3*7)} <-- uh oh, problem?
{if ($foo+$bar.test%$baz*134232+10+$b+10)}
{$foo|truncate:"`($fooTruncCount/$barTruncFactor-1)`"}
{assign var="foo" value="`($foo+$bar)`"}
We already allow parenthesis for objects, so I'm not sure if that's going to cause a problem with the parser or not. |
|
Back to top |
|
|