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

new simple for loop syntax
Goto page Previous  1, 2
 
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 -> Smarty 3
View previous topic :: View next topic  
Author Message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Fri Dec 04, 2009 1:46 am    Post subject: Reply with quote

Quote:
{loop $x from 100 to 1 by 10}


In this case you have no chance to protect yourself against wrong step directions when you are using variables. It will always loop.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Dec 04, 2009 1:54 am    Post subject: Reply with quote

I think the way we have it is good, but I'm still not 100% sure what problems auto-signing the step value may crop up. Can anyone think of a situation where it could cause problems?
Back to top
View user's profile Send private message Visit poster's website
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Fri Dec 04, 2009 2:19 am    Post subject: Reply with quote

morht,

just with a glance:

Code:
{for $x = 100 to 1 step 10}


I would expect nothing

Code:
{loop $x from 100 to 1 by 10}


I would expect it to count downwards.




U.Tews wrote:
I think the step direction could be determined automatically. If you want to protect yourself against unwanted step directions you can always do so by specifying it explicitly.


I would ask that you consider your words carefully. To "protect yourself" you have to add an optional piece? When that protection comes free or transparently with most other languages and that protection is what many people expect from using other languages?

here is my argument:
1. Most examples in this thread use literals, but most real world examples use variables, which give no immediate visual cue as far as the direction of the loop.
2. Very rarely have I seen for loops that are meant to count one direction sometimes, and another direction sometimes.
3. People don't read directions.

The tradeoff to me is this.

Auto switching: Things work great at first but user finds out down the road that only on certain occasions things go wrong. Especially when $end represents an absolute boundary. This is analogous to a home insurance company that only tells you after your house is destroyed that you had the wrong kind of insurance, but hey, you should have read the fine print.

No auto switching: User notices immediately that it's not iterating, user reads instructions and finds out that they should have used a step -1. User fixes the problem. Things go well from then on.


On the other hand, I understand your points, so if you decide to implement it the other way, I won't complain.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Dec 04, 2009 2:34 am    Post subject: Reply with quote

I think what he meant by "protect yourself" is that you can guarantee the direction of the step by specifying it explicitly.
Back to top
View user's profile Send private message Visit poster's website
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Fri Dec 04, 2009 2:37 am    Post subject: Reply with quote

mohrt wrote:
I think what he meant by "protect yourself" is that you can guarantee the direction of the step by specifying it explicitly.


i know. but when some one says "protect yourself" that implies a bad consequence.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Dec 04, 2009 2:50 am    Post subject: Reply with quote

I'm just thinking of this from more of a template designer standpoint, as opposed to a (more terse) development environment. If you do this:

{for $x = 5 to 1}

Would you expect the loop to count down or not? My first hunch is that looping downward is the intention. Now, something like this:

{for $x = $y to $z}

Looking at the template, you can't tell which direction the loop is going, but you can guarantee it WILL loop from $y to $z if auto-detection is on. I'm thinking because of the simplicity of the logic, the auto-detection is a welcome addition.

Now something like this:

{for $x=$min,$y=$max; $x<=abs($y); $x++}

It is probably better to handle the step explicitly, as this syntax is not so obvious what's going on.
Back to top
View user's profile Send private message Visit poster's website
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Fri Dec 04, 2009 3:40 am    Post subject: Reply with quote

i understand, you make some good points, maybe the benefits outweigh any possible problems.

mohrt wrote:
I think the way we have it is good, but I'm still not 100% sure what problems auto-signing the step value may crop up. Can anyone think of a situation where it could cause problems?


say programmer wants to do some pagination like

<< 5 6 7 8 9 10 11 12 13>>

and programmer does something like (pseudocode):

Code:

$startpage = max(1, $requestpage-4);
$endpage = min($num_pages , $requestpage+4);

for $p = $startpage  to $requestpage  - 1
   print $p

print $requestpage

for $p =  $requestpage + 1 to $endpage
  print $p



web user manipulates URL, gives $requestpage of -10000000, and it iterates over millions of negative numbers, even when programmer thought the min, max, and for loop gave them protection from that. Looking at the code at a glance, that outcome looks impossible to me.

I guess it's not so bad since templates don't actually manipulate data, which would potentially make not respecting those boundaries much worse since it could manipulate the wrong data. But, those are the type of situations i am talking about.


Last edited by douglassdavis on Fri Dec 04, 2009 4:40 am; edited 1 time in total
Back to top
View user's profile Send private message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Fri Dec 04, 2009 4:31 am    Post subject: Reply with quote

Can any one give a situation where you want the loop to go in one direction sometimes, and in another direction other times?
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Dec 04, 2009 4:41 am    Post subject: Reply with quote

You would think you always want the loop to go in the direction of left to right, whichever step that requires.

{for $x = 1 to 5}{$x}{/for}

12345

{for $x = 5 to 1}{$x}{/for}

54321
Back to top
View user's profile Send private message Visit poster's website
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Fri Dec 04, 2009 4:51 am    Post subject: Reply with quote

douglassdavis wrote:
Can any one give a situation where you want the loop to go in one direction sometimes, and in another direction other times?


by that, i meant when you are using variables, not literals for start and end (which applies to most cases I think).

Code:
{for $x = $a to $b}


When would you want this (representing a specific one for loop in a template) to go in one direction (+ or -) sometimes, in another direction sometimes? This is the inevitable consequence.



anyway... I think I stated my case, so i'll leave it alone Very Happy I understand the other side of the argument, if the step is automatic, i will just always add a step to prevent problems. I'm probably way too defensive of a programmer... But like magic quotes or register globals, when choosing between a little inconvenience now or unexpected consequences later, i'll take the inconvenience now. ok... leaving alone for real now. Laughing Arrow
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Dec 04, 2009 2:54 pm    Post subject: Reply with quote

I think we'll try the auto-step to start and see how it works.
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Dec 04, 2009 3:26 pm    Post subject: Reply with quote

Another issue we ran into, what syntax to use:

Code:
{for $x = 1 to 5 step 1 max 4}


or

Code:
{for $x = 1 to 5 step=1 max=4}


I think we'll support both but document one.
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Dec 04, 2009 5:41 pm    Post subject: Reply with quote

Sure:

This time I want to go up:

{for $x = 1 to 5}

This time I want to go down:

{for $x = 5 to 1}
Back to top
View user's profile Send private message 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 -> Smarty 3 All times are GMT
Goto page Previous  1, 2
Page 2 of 2

 
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