|
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 |
Jerc Smarty Rookie
Joined: 26 Jan 2010 Posts: 30 Location: Slovenia / Ljubljana
|
Posted: Thu Aug 06, 2015 11:40 am Post subject: {cycle} possible bug? |
|
|
I'm not sure if this is a bug or I am doing something wrong. I tried to control the cycle with 'advance' attribute. I set it to true when I want to change the color, but this happens on next iteration. Picture below explains my problem.
Smarty version 3.1.27.
PHP code:
Code: | $data = array(
array(
'title' => 'data 1',
'data' => array('a', 'b')
),
array(
'title' => 'data 2',
'data' => array('c')
),
array(
'title' => 'data 3',
'data' => array('d')
),
array(
'title' => 'data 4',
'data' => array('e', 'f', 'g')
),
array(
'title' => 'data 5',
'data' => array('h', 'i')
)
);
$smarty->assign('data', $data); |
Template:
Code: | <table>
<tr>
{foreach $data as $a}
<td colspan="{$a.data|count}" style="color:#FFF;background-color: #{cycle values='7689a9,152c55,051838' name='thead'}">{$a.title}</td>
{/foreach}
</tr>
<tr>
{assign var="advance" value=false}
{foreach $data as $a}
{foreach $a.data as $b}
<td style="color:#FFF;background-color: #{cycle values='7689a9,152c55,051838' name='tbody' advance=$advance}">{$b}</td>
{assign var="advance" value=false}
{/foreach}
{assign var="advance" value=true}
{/foreach}
</tr>
</table> |
Result:
Expected result:
|
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Thu Aug 06, 2015 2:23 pm Post subject: |
|
|
1. I don't see, where you are resetting the cycle.
2. You don't need to reassign $advance inside the cycle. Hell, you don't even need to use $advance at all. You may advance the cycle outside with "print=false".
3. You only need to set cycle values once, the rest of the time you may simply refer to it by name.
In short,
Code: | <table>
<tr>
{foreach $data as $a}
<th colspan="{$a.data|count}" style="color:#FFF;background-color: #{cycle values='7689a9,152c55,051838' name='thead'}">{$a.title}</th>
{/foreach}
</tr>
<tr>
{cycle name='thead' reset=true print=false advance=false}
{foreach $data as $a}
{foreach $a.data as $b}
<td style="color:#FFF;background-color: #{cycle name='thead' advance=false}">{$b}</td>
{/foreach}
{cycle name='thead' print=false}
{/foreach}
</tr>
</table> |
|
|
Back to top |
|
Jerc Smarty Rookie
Joined: 26 Jan 2010 Posts: 30 Location: Slovenia / Ljubljana
|
Posted: Fri Aug 07, 2015 5:48 am Post subject: |
|
|
I knew that there is a trick.
Thanks for your solution. |
|
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
|