我有一个滑块,列出了4
我想增加一行,这样它将显示2x4的产品。
为了实现这一点,我需要列出2种产品。
我真的试过修复它,但我的代码和我计算产品的方式有问题。
请看一看:`{if isset($products)}{include file="$tpl_dir./breadcrumb.tpl"}{assign var=计数值=0}
{foreach from=$products item=product name=products}
{if $count == 0}
<li style="position: relative;">
{/if}
<div id="ProductCon">
<div class="prodimage"><a href="{$product.link|escape:'htmlall':'UTF-8'}" class="product_img_link" title="{$product.name|escape:'htmlall':'UTF-8'}"><img src="{$link->getImageLink($product.link_rewrite, $product.id_image, 'home')}" alt="{$product.legend|escape:'htmlall':'UTF-8'}" {if isset($homeSize)} width="{$homeSize.width}" height="{$homeSize.height}"{/if} /></a></div>
<h3><a href="{$product.link|escape:'htmlall':'UTF-8'}" title="{$product.name|escape:'htmlall':'UTF-8'}">{$product.name|truncate:35:'...'|escape:'htmlall':'UTF-8'}</a></h3>
</div>
{if $count == 1}
</li>
{/if}
{function name='counter2'}
{$count+1}
{/function}
{/foreach}
</ul>
</div>
<!-`
您可能可以在foreach循环中检查索引值。下面是一个如何实现它的例子。它还检查列表中是否有奇数个产品,并关闭最后一个产品的li标签。希望这能帮助
<ul>
{foreach from=$products item=product name=products}
{if $smarty.foreach.products.first == true || $smarty.foreach.products.index % 2 == 0}
<li style="position: relative;">
{/if}
<div id="ProductCon">
<div class="prodimage"><a href="{$product.link|escape:'htmlall':'UTF-8'}" class="product_img_link" title="{$product.name|escape:'htmlall':'UTF-8'}"><img src="{$link->getImageLink($product.link_rewrite, $product.id_image, 'home')}" alt="{$product.legend|escape:'htmlall':'UTF-8'}" {if isset($homeSize)} width="{$homeSize.width}" height="{$homeSize.height}"{/if} /></a></div>
<h3><a href="{$product.link|escape:'htmlall':'UTF-8'}" title="{$product.name|escape:'htmlall':'UTF-8'}">{$product.name|truncate:35:'...'|escape:'htmlall':'UTF-8'}</a></h3>
</div>
{if $smarty.foreach.products.index % 2 == 1 || $smarty.foreach.products.last == true}
</li>
{/if}
{/foreach}
</ul>
Smarty不是一种(完整的)编程语言,它是一个模板引擎。有些事情是故意不允许的。
你应该使用一个计数器,这是smarty提供的道具,这样你就不必使用编程逻辑来实现你的目标。
对于分配,您需要使用一个特殊的智能功能智能分配。
对于smarty counter,这应该有效:
{counter start=0 skip=1 assign="count"}
{foreach from=$products item=product name=products}
{if $count == 0}
<li style="position: relative;">
{/if}
<div id="ProductCon">
<div class="prodimage">
<a href="{$product.link|escape:'htmlall':'UTF-8'}" class="product_img_link" title="{$product.name|escape:'htmlall':'UTF-8'}">
<img src="{$link->getImageLink($product.link_rewrite, $product.id_image, 'home')}" alt="{$product.legend|escape:'htmlall':'UTF-8'}" {if isset($homeSize)} width="{$homeSize.width}" height="{$homeSize.height}"{/if} />
</a>
</div>
<h3>
<a href="{$product.link|escape:'htmlall':'UTF-8'}" title="{$product.name|escape:'htmlall':'UTF-8'}">{$product.name|truncate:35:'...'|escape:'htmlall':'UTF-8'}</a>
</h3>
</div>
{if $count == 1}
</li>
{/if}
{counter}
{/foreach}