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

{cycle} possible bug?

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


Joined: 26 Jan 2010
Posts: 30
Location: Slovenia / Ljubljana

PostPosted: Thu Aug 06, 2015 11:40 am    Post subject: {cycle} possible bug? Reply with quote

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
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Aug 06, 2015 2:23 pm    Post subject: Reply with quote

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
View user's profile Send private message
Jerc
Smarty Rookie


Joined: 26 Jan 2010
Posts: 30
Location: Slovenia / Ljubljana

PostPosted: Fri Aug 07, 2015 5:48 am    Post subject: Reply with quote

I knew that there is a trick.

Thanks for your solution.
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 -> Bugs 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