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

A while loop that assigns values until null

 
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 -> Plugins
View previous topic :: View next topic  
Author Message
jarofgreen
Smarty Rookie


Joined: 09 Jul 2010
Posts: 14

PostPosted: Fri Sep 24, 2010 9:43 am    Post subject: A while loop that assigns values until null Reply with quote

I'm looking for a plug-in for Smarty 3 that does a while loop like the PHP code:

Code:
while($x = $y->getNextResult()) {
   ...
}


So it loops round until getNextResult() starts to return Null.

It would look something like this:

Code:
{whileassign from=$y->getNextResult() var=x}
  ... {$x} ...
{/whileassign}


Ideally, it would also assign counters to the smarty global like for does, so you can have

Code:
{whileassign from=$y->getNextResult() var=x name=foo}
  ...  {$x} ... {$smarty.while.foo.index} ...
{/whileassign}


Or at the very least:

Code:
{whileassign from=$y->getNextResult() var=x counter=count}
  ...  {$x} ... {$count} ...
{/whileassign}


Does anyone have such a thing, or can anyone point me to something similar I can use as guidance to write one myself? I'll release it under the BSD licence as part of Elastik (which you can find on SourceForge)

Thanks very much,
James
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Fri Sep 24, 2010 12:54 pm    Post subject: Reply with quote

This is already build in (not yet documented)

Code:
{while $x=$y->getNextResult()}
  ... {$x} ...
{/while}


or
Code:
{while $x>0} 
.... {$x=$x-1}....
{/while}


The {while} tag can be used with an assignment as in the first example or an if-condition like in the second example.
Back to top
View user's profile Send private message
jarofgreen
Smarty Rookie


Joined: 09 Jul 2010
Posts: 14

PostPosted: Fri Sep 24, 2010 1:30 pm    Post subject: Reply with quote

Cool, that's fantastic! And I have just realised I can use Cycle to do what I want so I don't need to get a count out of it.

Thanks for your prompt reply Smile
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 -> Plugins 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