Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

math in 2.6.0RC1

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
sagi
Smarty Regular


Joined: 30 Jul 2003
Posts: 43

PostPosted: Sun Aug 31, 2003 8:06 pm    Post subject: math in 2.6.0RC1 Reply with quote

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
View user's profile Send private message
boots
Administrator


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

PostPosted: Sun Aug 31, 2003 11:13 pm    Post subject: Reply with quote

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
Code:
{$patcount*3}

which works as expected.

HTH
Back to top
View user's profile Send private message
sagi
Smarty Regular


Joined: 30 Jul 2003
Posts: 43

PostPosted: Sun Aug 31, 2003 11:26 pm    Post subject: Reply with quote

I understand.

Funny because the example at: http://smarty.php.net/manual/en/language.math.php actually uses whitespace.
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Sun Aug 31, 2003 11:54 pm    Post subject: Reply with quote

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
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Mon Sep 01, 2003 5:54 am    Post subject: Reply with quote

whitespaces work in {math} but not in simple math. they are two totally different things.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
sagi
Smarty Regular


Joined: 30 Jul 2003
Posts: 43

PostPosted: Mon Sep 01, 2003 10:22 am    Post subject: Reply with quote

I know. But the example for *simple math* uses whitespace.
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Mon Sep 01, 2003 11:12 am    Post subject: Reply with quote

aah, i see. i fixed this in cvs. the fixed examples will become online with 2.6.0-RC2.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
sagi
Smarty Regular


Joined: 30 Jul 2003
Posts: 43

PostPosted: Sun Sep 07, 2003 4:05 pm    Post subject: Reply with quote

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
View user's profile Send private message
boots
Administrator


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

PostPosted: Sun Sep 07, 2003 4:25 pm    Post subject: Reply with quote

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
View user's profile Send private message
sagi
Smarty Regular


Joined: 30 Jul 2003
Posts: 43

PostPosted: Sun Sep 07, 2003 4:41 pm    Post subject: Reply with quote

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
View user's profile Send private message
boots
Administrator


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

PostPosted: Sun Sep 07, 2003 6:35 pm    Post subject: Reply with quote

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
View user's profile Send private message
silicate
Smarty n00b


Joined: 09 Sep 2003
Posts: 4
Location: Toronto, Ontario, Canada

PostPosted: Tue Sep 09, 2003 2:18 pm    Post subject: Math Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
boots
Administrator


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

PostPosted: Tue Sep 09, 2003 2:48 pm    Post subject: Reply with quote

Simple math is not supported in 2.5.0. Instead of using {assign}, use the {math} plugin instead.
Back to top
View user's profile Send private message
silicate
Smarty n00b


Joined: 09 Sep 2003
Posts: 4
Location: Toronto, Ontario, Canada

PostPosted: Thu Sep 11, 2003 3:33 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Thu Sep 11, 2003 6:38 am    Post subject: Reply with quote

you don't need a counter in your example above.
use {$smarty.section.i.index},
see: http://smarty.php.net/manual/en/section.property.index.php
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General 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