I am using smarty and bootstrap,
I am building an image gallery so my markup ends up being as posted below.
I know I can loop over my categories as {foreach $categories as $category}{/foreach}
But as you can see by the markup posted below I need to wrap the in groups of four. That's the part I don't know how to do.
Also, note that there may not always be a multiple of four, eg there could be 15 categories.
How should I do this?
<div class="row-fluid">
<ul class="thumbnails">
<li class="span3">
<div class="thumbnail">
<a href="#" class="thumbnail"><img src="http://placehold.it/260x180" alt=""></a>
<div class="caption">
<h3>Thumbnail label</h3>
<p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida
at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
<p><a href="#" class="btn btn-primary">Action</a> <a href="#" class="btn">Action</a></p>
</div>
</div>
</li>
<li class="span3">
<div class="thumbnail">
<a href="#" class="thumbnail"><img src="http://placehold.it/260x180" alt=""></a>
<div class="caption">
<h3>Thumbnail label</h3>
<p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida
at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
<p><a href="#" class="btn btn-primary">Action</a> <a href="#" class="btn">Action</a></p>
</div>
</div>
</li>
<li class="span3">
<div class="thumbnail">
<a href="#" class="thumbnail"><img src="http://placehold.it/260x180" alt=""></a>
<div class="caption">
<h3>Thumbnail label</h3>
<p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida
at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
<p><a href="#" class="btn btn-primary">Action</a> <a href="#" class="btn">Action</a></p>
</div>
</div>
</li>
<li class="span3">
<div class="thumbnail">
<a href="#" class="thumbnail"><img src="http://placehold.it/260x180" alt=""></a>
<div class="caption">
<h3>Thumbnail label</h3>
<p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida
at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
<p><a href="#" class="btn btn-primary">Action</a> <a href="#" class="btn">Action</a></p>
</div>
</div>
</li>
</ul>
</div>
<div class="row-fluid">
<ul class="thumbnails">
<li class="span3">
<div class="thumbnail">
<a href="#" class="thumbnail"><img src="http://placehold.it/260x180" alt=""></a>
<div class="caption">
<h3>Thumbnail label</h3>
<p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida
at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
<p><a href="#" class="btn btn-primary">Action</a> <a href="#" class="btn">Action</a></p>
</div>
</div>
</li>
<li class="span3">
<div class="thumbnail">
<a href="#" class="thumbnail"><img src="http://placehold.it/260x180" alt=""></a>
<div class="caption">
<h3>Thumbnail label</h3>
<p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida
at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
<p><a href="#" class="btn btn-primary">Action</a> <a href="#" class="btn">Action</a></p>
</div>
</div>
</li>
<li class="span3">
<div class="thumbnail">
<a href="#" class="thumbnail"><img src="http://placehold.it/260x180" alt=""></a>
<div class="caption">
<h3>Thumbnail label</h3>
<p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida
at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
<p><a href="#" class="btn btn-primary">Action</a> <a href="#" class="btn">Action</a></p>
</div>
</div>
</li>
<li class="span3">
<div class="thumbnail">
<a href="#" class="thumbnail"><img src="http://placehold.it/260x180" alt=""></a>
<div class="caption">
<h3>Thumbnail label</h3>
<p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida
at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
<p><a href="#" class="btn btn-primary">Action</a> <a href="#" class="btn">Action</a></p>
</div>
</div>
</li>
</ul>
</div>
You can use name
for the foreach
loop and then check the iteration number, something like this:
{foreach from=$array item=var name=myloop}
{if $smarty.foreach.myloop.iteration % 4 == 1}
{* this is the first in a group of four *}
{elseif $smarty.foreach.myloop.iteration % 4 == 0}
{* this is the last in a group of four *}
{else}
{* this is the second or the third *}
{/if}
{/foreach}
Read through the documentation of foreach
for more details. Specifically look at iteration
and index
.
As it seems you are using Smarty v3 , you can directly use @iteration
on the variable :
{foreach $array as $var}
{if $var@iteration %4 == 1}
<div class="row">
{/if}
<div class="span3"><!-- ... --></div>
{if $var@iteration %4 == 0}
</div><!-- /div.row -->
{/if}
{/foreach}
The other thing is, the .thumbnails
is meant to be used as a .row
but it allows more than the classic 12 cells.
So I don't know if it handles .row-fluid
very well. That's why your workaround needs several hard -rows.
The best course of action would be to define the width of the spans yourself, or wait for a .thumbnails-fluid
implementation like this one : Demo (jsfiddle)
.thumbnails.thumbnails-fluid { margin-left: 0; }
.thumbnails.thumbnails-fluid > [class*="span"] {
display: block;
float: left;
width: 100%;
min-height: 1px;
margin-left: 2.127659574468085%;
*margin-left: 2.074468085106383%;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.thumbnails.thumbnails-fluid .span12 { width: 100%; *width: 99.94680851063829%; }
.thumbnails.thumbnails-fluid .span11 { width: 91.48936170212765%; *width: 91.43617021276594%; }
.thumbnails.thumbnails-fluid .span10 { width: 82.97872340425532%; *width: 82.92553191489361%; }
.thumbnails.thumbnails-fluid .span9 { width: 74.46808510638297%; *width: 74.41489361702126%; }
.thumbnails.thumbnails-fluid .span8 { width: 65.95744680851064%; *width: 65.90425531914893%; }
.thumbnails.thumbnails-fluid .span7 { width: 57.44680851063829%; *width: 57.39361702127659%; }
.thumbnails.thumbnails-fluid .span6 { width: 48.93617021276595%; *width: 48.88297872340425%; }
.thumbnails.thumbnails-fluid .span5 { width: 40.42553191489362%; *width: 40.37234042553192%; }
.thumbnails.thumbnails-fluid .span4 { width: 31.914893617021278%; *width: 31.861702127659576%; }
.thumbnails.thumbnails-fluid .span3 { width: 23.404255319148934%; *width: 23.351063829787233%; }
.thumbnails.thumbnails-fluid .span2 { width: 14.893617021276595%; *width: 14.840425531914894%; }
.thumbnails.thumbnails-fluid .span1 { width: 6.382978723404255%; *width: 6.329787234042553%; }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.