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

Please help on foreach iteration

 
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 -> General
View previous topic :: View next topic  
Author Message
prestondocks
Smarty n00b


Joined: 13 Apr 2016
Posts: 4

PostPosted: Wed Apr 13, 2016 5:52 pm    Post subject: Please help on foreach iteration Reply with quote

Can someone tell me why the following code does not generate the output you would expect. The actual output is shown below the code. Notice that the second to last <div class="col-md-4"......> is not wrapped in a row div, it's just floating free.

Code:
{foreach $products as $product}
       
       
        {if $product@first || $product@iteration is div by 3}
            <div class="row"> 
        {/if}
       
        <div class="col-md-4">{$product->name}</div>

        {if $product@last || $product@iteration is div by 3}
            </div>   
        {/if}
       
    {/foreach}


Code:
     <div class="row"> 
               
        <div class="col-md-4">Product 1</div>
               
        <div class="col-md-4">Products 2</div>
     
        <div class="col-md-4">Products 2</div>

                    </div>         
       
                    <div class="row"> 
               
        <div class="col-md-4">Products 2</div>
   
        <div class="col-md-4">Products 2</div>

        <div class="col-md-4">Products 2</div>

                    </div>   
 
        <div class="col-md-4">Products 2</div>

                    <div class="row"> 
               
        <div class="col-md-4">Products 2</div>

                    </div>   
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Wed Apr 20, 2016 12:20 am    Post subject: Reply with quote

For sure the output which you provided is not what the loop did generate.
Your loop did generate the new <div class="row"> on iteration 3 before the old was close. The new <div class="row"> must be generated when iteration 4 starts.

Code:
{foreach $products as $product}
       
       
        {if $product@first || $product@index div by 3}
            <div class="row"> 
        {/if}
       
        <div class="col-md-4">{$product->name}</div>

        {if $product@last || $product@iteration is div by 3}
            </div>   
        {/if}
       
    {/foreach}

As index starts at 0 this should generate the right result.
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 -> General 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