2

我想用 smarty 实现这样的列表。

<ul>
 <li>
   <a>img1</a>
   <a>img2</a>
   <a>img3</a>
   <a>img4</a>
 </li>
 <li>
   <a>img5</a>
   <a>img6</a>
   <a>img7</a>
   <a>img8</a>
 </li>
<li>
   <a>img9</a>
   <a>img10</a>
   <a>img11</a>
   <a>img12</a>
 </li>
</ul>

使用此示例代码

<ul class="bullet">
    {foreach from=$manufacturers item=manufacturer name=manufacturer_list}
        {if $smarty.foreach.manufacturer_list.index < 4}
      <li class="{if $smarty.foreach.manufacturer_list.last}last_item{elseif $smarty.foreach.manufacturer_list.first}first_item{else}item{/if}">   

        <a href="{$link->getmanufacturerLink($manufacturer.id_manufacturer, $manufacturer.link_rewrite)}" title="{l s='More about' mod='blockmanufacturer'}{$manufacturer.name}"> <img src="{$img_manu_dir}{$manufacturer.id_manufacturer}.jpg"><span>{$manufacturer.name}<span></a>
      </li>
    {/if}
{/foreach}

首先使用给定的数组 $manufacturers 它将在<li>最多 4 次内循环并创建 4 <img>。然后,当它到达第 4 个索引时,它将创建一个新<li>标签。

谢谢您的帮助!

4

2 回答 2

5

您想在每个 li 标签中显示 4 张图像吗?

<ul>
{foreach from=$manufacturers item=manufacturer name=manufacturer_list}
  {if $smarty.foreach.manufacturer_list.index % 4 == 0}
    <li>
  {/if}

  <a><img></a>

  {if $smarty.foreach.manufacturer_list.index % 4 == 0 || $smarty.foreach.manufacturer_list.last}
    </li>
  {/if}
{/foreach}
</ul>
于 2012-06-17T09:13:11.507 回答
2

我是 Smarty3 语法,您可以执行以下操作:

{$data = [
    "alpha", "bravo", "charlie", "delta", "echo", "foxtrot", "golf", 
    "hotel", "india", "juliet", "kilo", "lima", "mike", "november", 
    "oscar", "papa", "quebec", "romeo", "sierra", "tango", "uniform", 
    "victor", "whiskey", "x-ray", "yankee", "zulu"
]}

{foreach $data as $value}
  {if $value@first}
    <ul>
      <li>
  {elseif ($value@iteration - 1) is div by 4}
      </li><li>
  {/if}

  <span>{$value}</span>

  {if $value@last}
      </li>
    </ul>
  {/if}
{/foreach}

请注意,由于将整体嵌套<ul>在 foreach 循环中,因此它不会显示是否没有要循环的数据。

于 2012-06-17T09:58:54.703 回答