View previous topic :: View next topic |
Author |
Message |
sagi Smarty Regular
Joined: 30 Jul 2003 Posts: 43
|
Posted: Sun Aug 31, 2003 8:06 pm Post subject: math in 2.6.0RC1 |
|
|
According to the changelog there's now support for basic math inside the functions. I saw the examples in the manual and tried to do the following:
spacer should be: {$pcatcount * 3}
where $pcatcount is 3.
I get the following error:
Fatal error: Smarty: [in header.tpl line 259]: syntax error: invalid attribute name: '*' (Smarty_Compiler.class.php, line 1412) in /home/sagi/smarty/Smarty-2.6.0-RC1/libs/Smarty_Compiler.class.php on line 2040
Do I need to enable the math support anywhere?
Last edited by sagi on Sun Aug 31, 2003 11:26 pm; edited 1 time in total |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sun Aug 31, 2003 11:13 pm Post subject: |
|
|
The problem is the spaces--as far as the parser is concerned, any whitespace ends the expression. Thus, your issue can be solved simply with
which works as expected.
HTH |
|
Back to top |
|
sagi Smarty Regular
Joined: 30 Jul 2003 Posts: 43
|
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sun Aug 31, 2003 11:54 pm Post subject: |
|
|
So it does! To be honest, I'm not sure if this is the expected behviour or not--I only know that in practice, those whitespaces don't work. Which is sort of inline with other features such as modifiers which also don't permit whitespace.
On the other hand, if this was a vote, I would vote for whitespace support both in maths and modifiers. I don't think it would be overly hard to do (I did a trivial patch for 2.5.0 that introduced whitespace into modifiers awhile back) though it would undoubtedly be yet another thing to slow the parser down. I doubt it would have much affect but I remember that people were sensitive to my suggestion for whitespace in modifiers.
At any rate, either the functionality for math should change or the manual should be updated. |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Mon Sep 01, 2003 5:54 am Post subject: |
|
|
whitespaces work in {math} but not in simple math. they are two totally different things. |
|
Back to top |
|
sagi Smarty Regular
Joined: 30 Jul 2003 Posts: 43
|
Posted: Mon Sep 01, 2003 10:22 am Post subject: |
|
|
I know. But the example for *simple math* uses whitespace. |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Mon Sep 01, 2003 11:12 am Post subject: |
|
|
aah, i see. i fixed this in cvs. the fixed examples will become online with 2.6.0-RC2. |
|
Back to top |
|
sagi Smarty Regular
Joined: 30 Jul 2003 Posts: 43
|
Posted: Sun Sep 07, 2003 4:05 pm Post subject: |
|
|
how can i do something like:
rowspan: {$products|@count*2-1}
i get a fatal error when doing that, i also tried {`$products|@count`*2-1} |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sun Sep 07, 2003 4:25 pm Post subject: |
|
|
AFAIK, you will have to do an intermediate assignment of the count value:
Code: | {assign var=cnt value=$products|@count}
{$cnt*2-1} |
My guess is that the parser only allows math against variables, so once the parser starts with the modifier it no longer expects math ops. |
|
Back to top |
|
sagi Smarty Regular
Joined: 30 Jul 2003 Posts: 43
|
Posted: Sun Sep 07, 2003 4:41 pm Post subject: |
|
|
Or maybe I should just keep using the math function for that?
I saw that right now it runs those regex/evals everytime i use the math function so it probably makes it very slow. I also saw the alternative in one of the forums here, are there any plans to make the alternative the default in future releases? if not, can i safely replace the original math with it? |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sun Sep 07, 2003 6:35 pm Post subject: |
|
|
IIR, the new builtin math in 2.6+ (as opposed to the plugin math) runs those regex's only at compile time, much like the plugin compile time math function in the plugin forum that you mention. My recollection is that the compile time math function will not end up as part of the distribution, but it should still work if you need it. |
|
Back to top |
|
silicate Smarty n00b
Joined: 09 Sep 2003 Posts: 4 Location: Toronto, Ontario, Canada
|
Posted: Tue Sep 09, 2003 2:18 pm Post subject: Math |
|
|
I was trying to use a simple counter inside a section statement, but when I try to perform math on the index variable I am getting string cat'ing.
Basic configuration info:
Smarty V 2.5.0
Alternate delimiters '{{' and '}}'
Example of what I am trying to do:
Code: | {{ assign var="MyIndex" value="0" }}
{{ section name=i loop=$fields }}
{{ if SomeTestConditionPasses }}
Value #{{$MyIndex}}:"{{$fields.[i]}}"<BR>
{{ assign var="MyIndex" value="$MyIndex+1" }}
{{ /if }}
{{ /section }}
|
Example Output:
Code: | Value #0:"blah"
Value #0+1:"heh"
Value #0+1+1:"test"
|
I tried putting in backticks as well as removing the whitespaces from the value assignment but neither one worked. Should I implictly call {{math}} to do this?
Thanks,
Matthew |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue Sep 09, 2003 2:48 pm Post subject: |
|
|
Simple math is not supported in 2.5.0. Instead of using {assign}, use the {math} plugin instead. |
|
Back to top |
|
silicate Smarty n00b
Joined: 09 Sep 2003 Posts: 4 Location: Toronto, Ontario, Canada
|
Posted: Thu Sep 11, 2003 3:33 am Post subject: |
|
|
Thanks Boots,
I decided to rethink the data that I was passing to the smarty template after reading in the manual about the {math} action. Words like "slow" are bad. heh.
Thanks again.
Matthew |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
|
Back to top |
|
|