karabey
karabey

Reputation: 57

Smarty Foreach Columns

I am converting a Regular Template to Responsive One. I have some trouble at generating columns.

My Code which is not working correctly

<div class="row-fluid">
    {foreach name=aussen item=module_data from=$module_content}
    {php} $col++; {/php}
        <div class="span4">
            <h2>Heading</h2>
            <p>Text</p>
            <p><a class="btn" href="#">View details »</a></p>
        </div>
    {php} if ($col>=4) {$col=0;echo '';}{/php}
    {/foreach}
</div>

The Responsive Theme has this structure

<div class="span9">
    <div class="row-fluid">
        <div class="span4">
          <h2>Heading</h2>
          <p>Text</p>
          <p><a class="btn" href="#">Button &raquo;</a></p>
        </div>
        <div class="span4">
          <h2>Heading</h2>
          <p>Text</p>
          <p><a class="btn" href="#">Button &raquo;</a></p>
        </div>
        <div class="span4">
          <h2>Heading</h2>
          <p>Text</p>
          <p><a class="btn" href="#">Button &raquo;</a></p>
        </div>
    </div>
    <div class="row-fluid">
        <div class="span4">
          <h2>Heading</h2>
          <p>Text</p>
          <p><a class="btn" href="#">Button &raquo;</a></p>
        </div>
        <div class="span4">
          <h2>Heading</h2>
          <p>Text</p>
          <p><a class="btn" href="#">Button &raquo;</a></p>
        </div>
        <div class="span4">
          <h2>Heading</h2>
          <p>Text</p>
          <p><a class="btn" href="#">Button &raquo;</a></p>
        </div>
    </div>
</div>

How can I rewrite My Code to generate a Row with the Class "row-fluid" each time if each Row has reached maximum give columens of 3, 4 or 5?

Upvotes: 0

Views: 606

Answers (1)

IMSoP
IMSoP

Reputation: 97996

You can find out which iteration of a {foreach} loop you are in using the iteration and index properties as described in the Smarty 3 manual (or the equivalent page for Smarty 2).

In this case, you want to do something different "every 4 iterations", so you can use Smarty's is even by 4 construct. Your foreach loop has a name of aussen, so the variable to test is $smarty.foreach.aussen.iteration.

Alternatively, you can use your existing {$col} variable (although see my comment above about not using {php} tags): if the column number is zero, open a container div to start a new row; if it is 4, close the container div to mark the row's end. This approach will also require you to spot if you are on the last row of the loop, in case it is not divisible by 4, so you always close the row: {if $col==4 or $smarty.foreach.aussen.last}</div>{/if}.

Upvotes: 2

Related Questions