|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
Smarty Pants Smarty Rookie
Joined: 29 Apr 2003 Posts: 7 Location: Winnipeg, Manitoba
|
Posted: Wed Apr 30, 2003 11:08 pm Post subject: problems looping through negative values in section loops |
|
|
I want to be able to perform the following:
for b=$limit; b>=-$limit; b--
for a=-$limit; a<=$limit; a++
print (a,b)
end for
end for
so if limit=1 then the following table would be the result:
(-1,1) (0,1) (1,1)
(-1,0) (0,0) (1,0)
(-1,-1) (0,-1) (1,-1)
I don't know if I'm doing this right, but here's my code:
the result:<br>
<table border=1>
{section name=b loop=$limit start=$limit max=-$limit step=-1}
<tr>
{section name=a loop=$limit start=-$limit max=$limit step=1}
<td>{$smarty.section.b.index},{$smarty.section.a.index}</td>
{/section}
</tr>
{/section}
</table>
I'm having problems ending with a negative value in the first loop and starting with a negative value in the second loop.
hopefully someone can shed some light on this for me...thx _________________ 'we are the music makers, we are the dreamers of dreams" -Charlie and the chocolate factory |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu May 01, 2003 2:15 am Post subject: |
|
|
Someone else may be able to give you a better answer but in general, I don't think that {section} is a gerneral replacement for php's for loops. Section does not appear to support -ve indexes and I've always felt that it could have been designed a bit better. BUT, I probably always think that because I want them to act like normal for loops!
Also, without math support in Smarty, tricky loops (those that setup data) should really be kept in PHP. Can you not set up an array in PHP (ie. do your loop in PHP) and then send the array to smarty?
Like I said, not much of an answer, but sometimes I think it is better to take a different tact then to make something work where it just doesn't want to work
good luck |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu May 01, 2003 6:59 am Post subject: |
|
|
Just thought I'd mention that if you really must do this sort of thing in template, you can use {php} ... {/php} blocks to roll your own loops.
Code: |
$smarty->assign('limit', 2);
----------------
<table>
{php}
$limit=$this->get_template_vars('limit');
for ($b=$limit; $b>=-$limit; $b--):
{/php}
<tr>
{php}
for ($a=-$limit; $a<=$limit; $a++):
$this->assign('a', $a);
$this->assign('b', $b);
{/php}
<td>({$a},{$b})</td>
{php}endfor;{/php}
</tr>
{php}endfor;{/php}
</table> |
As you can see, it ain't pretty!!
Note the use of the alternate control-structure syntax for "readability" (See: http://www.php.net/manual/en/control-structures.alternative-syntax.php).
Output:
Quote: | (-2,2) (-1,2) (0,2) (1,2) (2,2)
(-2,1) (-1,1) (0,1) (1,1) (2,1)
(-2,0) (-1,0) (0,0) (1,0) (2,0)
(-2,-1) (-1,-1) (0,-1) (1,-1) (2,-1)
(-2,-2) (-1,-2) (0,-2) (1,-2) (2,-2) |
Have fun! |
|
Back to top |
|
Smarty Pants Smarty Rookie
Joined: 29 Apr 2003 Posts: 7 Location: Winnipeg, Manitoba
|
Posted: Thu May 01, 2003 8:49 am Post subject: |
|
|
wow, thanks for alternative! _________________ 'we are the music makers, we are the dreamers of dreams" -Charlie and the chocolate factory |
|
Back to top |
|
Smarty Pants Smarty Rookie
Joined: 29 Apr 2003 Posts: 7 Location: Winnipeg, Manitoba
|
Posted: Fri May 02, 2003 3:09 am Post subject: smarty function |
|
|
I'm a newbie with Smarty.... so, I was just wondering if this is something you could create a smarty function for.... something that would act more like a for loop and suppot negative loops. _________________ 'we are the music makers, we are the dreamers of dreams" -Charlie and the chocolate factory |
|
Back to top |
|
|
|
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
|
|