|
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 |
prestondocks Smarty n00b
Joined: 13 Apr 2016 Posts: 4
|
Posted: Wed Apr 13, 2016 5:52 pm Post subject: Please help on foreach iteration |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Wed Apr 20, 2016 12:20 am Post subject: |
|
|
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 |
|
|
|
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
|