在动态数据库加载代码中包含静态html图像

问题描述 投票:1回答:1

我在下面的代码中将产品图片加载到Prestashop的产品页面的长描述中,它很适合将所有产品图像加载到彼此之下,我想在每两个图像之后添加一个“徽标”分隔符,下面是示例图片

{foreach from=$product.images item=image}
     <li>
        <img
          src="{$image.bySize.thickbox_default.url}"
          alt="{$image.legend}"
          title="{$image.legend}"
          width="100%"
          itemprop="image"
        >
      </li>
    {/foreach}  

enter image description here

php html prestashop smarty
1个回答
2
投票

是的,可以通过在循环中添加if条件在每个产品图像后添加徽标,如下所示:

{foreach from=$product.images item=image name=product_image}
    <li>
        <img
            src="{$image.bySize.thickbox_default.url}"
            alt="{$image.legend}"
            title="{$image.legend}"
            width="100%"
            itemprop="image"
        >
      </li>

      {if $smarty.foreach.product_image.index % 2 === 1}
          <img class="logo" src="/img/logo-separator.png" />
      {/if}
{/foreach} 

首先,只需在产品图像的name=product_image循环上添加foreach,通过使用smarty获取每个图像的索引,然后检查foreach项目的索引,并在每隔一个图像后添加$smarty.foreach.product_image.index % 2 === 1的徽标

© www.soinside.com 2019 - 2024. All rights reserved.