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

simple for loop (much better than section)
Goto page 1, 2  Next
 
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 -> Feature Requests
View previous topic :: View next topic  
Author Message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Mon Jan 21, 2008 3:29 am    Post subject: simple for loop (much better than section) Reply with quote

I think smarty should have a simple for loop.

{for name=myloop index=counter start=5 end=10 step=2}
{$counter}
{/for}

would print

5
7
9


I have used section to do the same thing, but it seems as though section is overly complicated to do this. For example from the man page:

loop - Value to determine the number of loop iterations
max - Sets the maximum number of times the section will loop.

for a beginner looking at this, it can be confusing because they look like they are saying almost the same thing.

Also other examples:

{section name=foo start=10 loop=20 step=2}
{$smarty.section.foo.index}
{/section}
<hr />
{section name=bar loop=21 max=6 step=-2}
{$smarty.section.bar.index}
{/section}

The above example will output:

10 12 14 16 18
<hr />
20 18 16 14 12 10

why is the syntax different when you are stepping backwards as opposed to going forwards? I know loop is always the high value. But when going down, you have to say how many times you want it to iterate (max), when going up, you have to give a starting value. Not very consistent.

Also, in the second case, the loop attribute does NOT determine the "number of loop iterations" as stated in the manual at the top of the page.

And, Instead of using $smarty.section.bar.index, why can't it just be a simple smarty variable?

In my opinion, the reason why section can seem inconsistent, is that is trying to be too much.

It would be great to have a simple for loop added to smarty.

I will make it and share the code if it will be added to the distribution.


Last edited by douglassdavis on Mon Jan 21, 2008 12:06 pm; edited 1 time in total
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Mon Jan 21, 2008 3:41 am    Post subject: Reply with quote

There is a {foreach} loop.
Back to top
View user's profile Send private message Visit poster's website
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Mon Jan 21, 2008 12:06 pm    Post subject: Reply with quote

mohrt wrote:
There is a {foreach} loop.


just like in PHP, a foreach is not the same as for.

A for loop is used simply for counting from one number to another.

While a foreach loop is used to loop through each element of an array and get key/value pairs.
Back to top
View user's profile Send private message
Celeb
Administrator


Joined: 17 Apr 2007
Posts: 1025
Location: Vienna

PostPosted: Mon Jan 21, 2008 1:55 pm    Post subject: Reply with quote

Why do you need to count from one number to another in Smarty?
_________________
Darn computers always do what I tell them to instead of what I want them to do.
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Mon Jan 21, 2008 3:59 pm    Post subject: Reply with quote

There is also a function called {counter} that will do exactly what you are trying to achieve with the {for} loop.
Back to top
View user's profile Send private message Visit poster's website
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Tue Jan 22, 2008 12:57 am    Post subject: Reply with quote

mohrt wrote:
There is also a function called {counter} that will do exactly what you are trying to achieve with the {for} loop.


No. It will not. let me tell you why.

{counter} is not for blocks.

{counter} does not allow you to repeat a block in a loop.

Counter, by itself does not allow you to repeat $x number of times. You already have to know how many times you want it to increment when you write the template, so you can add that many {counter}s to your template. Example from the web page: http://www.phpfreaks.com/smarty_manual/page/language.function.counter.html

{counter start=0 skip=2}<br />
{counter}<br />
{counter}<br />
{counter}<br />

This increments 3 times. If I want it to increment 10 times instead, I have to put:

{counter start=0 skip=2}<br />
{counter}<br />
{counter}<br />
{counter}<br />
{counter}<br />
{counter}<br />
{counter}<br />
{counter}<br />
{counter}<br />
{counter}<br />
{counter}<br />


Or, I have to put it inside a larger loop. Either of which ruins the point of using a {counter} as a mechanism for repetition.
Back to top
View user's profile Send private message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Tue Jan 22, 2008 1:18 am    Post subject: Reply with quote

Celeb wrote:
Why do you need to count from one number to another in Smarty?



As one reason, to do pagination. Say, I'm on page 10, I may want to put:

First Previous 7 8 9 10 11 12 13 Next Last

But, really, from a more general perspective I suspect the language designers knew people would want to count from one number to another, that is why they were wise enough to add a {counter} tag and add the counting capabilities to {section}.

I'm just saying, for all of the reasons I've previously mentioned, that there is a more simple, cleaner, more consistent way to do it.
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Tue Jan 22, 2008 2:20 am    Post subject: Reply with quote

You can handle all of your counting and looping with {section} and {foreach}, and it was decided not to add third function such as {for} just to make counting slightly easier. You could make a block function yourself and submit it to the plugins/wiki if you feel inclined. There is also a SmartyPaginate plugin for paginating with ease.
Back to top
View user's profile Send private message Visit poster's website
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Tue Jan 22, 2008 2:40 am    Post subject: Reply with quote

mohrt wrote:
You can handle all of your counting and looping with {section} and {foreach}, and it was decided not to add third function such as {for} just to make counting slightly easier. You could make a block function yourself and submit it to the plugins/wiki if you feel inclined. There is also a SmartyPaginate plugin for paginating with ease.


I know it -can- be done with {section}. I would argue that {section} makes it harder instead of easier, and also makes the syntax inconsistent for the reasons I mentioned. But, I respect the designer's decision.

Also, I will look at SmartyPaginate.
Back to top
View user's profile Send private message
japoani18
Smarty n00b


Joined: 16 Mar 2008
Posts: 1

PostPosted: Sun Mar 16, 2008 11:12 am    Post subject: The Code Is simple Reply with quote

{section name=foo start=1 loop=$nr step=1}
{$smarty.section.foo.index}
{/section}

where $nr is of course a variable from php !
_________________
Ubuntu HowTo
Back to top
View user's profile Send private message
AtomicPenguin
Smarty Rookie


Joined: 26 Nov 2007
Posts: 10

PostPosted: Fri Mar 21, 2008 1:16 am    Post subject: Reply with quote

I agree wholeheartedly with you, DouglassDavis - I find the section syntax rather confusing. A standard for-loop option would be far simpler in some situations.
_________________
- A.P.
Back to top
View user's profile Send private message Visit poster's website
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Mon Mar 31, 2008 12:25 am    Post subject: Reply with quote

AtomicPenguin wrote:
I agree wholeheartedly with you, DouglassDavis - I find the section syntax rather confusing. A standard for-loop option would be far simpler in some situations.


Yes and I could be wrong, but I am thinking a well designed foreach and for could do everything that a section could do but be less confusing.
Back to top
View user's profile Send private message
PoolSnoopy
Smarty n00b


Joined: 16 Jul 2008
Posts: 1

PostPosted: Wed Jul 16, 2008 3:23 pm    Post subject: Reply with quote

I also think that section is implemented in an non-intuitive way. I don't use smarty a lot, but had to recently.
I wanted to create a select field with the upcoming 10 years as options. My first attempt was to assign the startyear in PHP and let section generate the rest. Following the documentation I thought that this would be the way to go:
Code:
{section name="year" loop=10 start=$startyear}
Of course it is not.
The "right" way is:
Code:
{section name="year" loop=`$startyear+10` start=$startyear}
Now please step forward and tell me that's an intuitive way to create loops.
To make it more readable I actually stuck with
Code:
{section name="year" loop=10 start=0}
and used
Code:
{$smarty.section.year.index+$startyear}
in the loop.
So I would also vote for a simple for-loop anytime. Wink

just my 0.02€
Back to top
View user's profile Send private message
weisjohn
Smarty n00b


Joined: 01 Dec 2009
Posts: 1

PostPosted: Tue Dec 01, 2009 5:22 pm    Post subject: Reply with quote

@PoolSnoopy - thanks for your comment...

I would argue, very strongly, that Smarty needs a simple {for}:

* It is intuitive, while {section} is not.
* I had to google "smarty simple for loop" to figure out how to do it.
* The first line on the {section} docs page says:

Quote:
A {section} is for looping over arrays of data, ...


If I don't have an array of data, it would not be logical for me to think of using what is described as looping structure that works with arrays!

You guys (Smarty devs) might want to checkout Django Templates... I know it's not PHP but their templating syntax and API structure are so intuitive...
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Tue Dec 01, 2009 5:48 pm    Post subject: Reply with quote

Smarty 3 has a {for} tag added for looping (replacement for {section} tag):
{for $x=0, $y=count($foo); $x<$y; $i++} .... {/for}
Any number of statements can be used separated by comma as the first
inital expression at {for}.
Back to top
View user's profile Send private message
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 -> Feature Requests All times are GMT
Goto page 1, 2  Next
Page 1 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