我有一个列表4
我想添加一个额外的行,以便显示2x4产品。
为了实现这一目标,我需要列出2个产品。
我确实试图修复它,但我的代码有问题以及我如何计算我的产品。
请看一下:`{if isset($ products)} {include file =" $ tpl_dir。/ breadcrumb.tpl"} {assign var = count value = 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>
<!-`
答案 0 :(得分:0)
Smarty不是一种(完整的)编程语言,它是一种模板引擎。有些事情是故意不允许的。
你应该使用counter这是一个聪明的提议,以便你不必使用编程逻辑来实现你的目标。
对于作业,您需要使用特殊的Smarty函数smarty assign。
{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}
答案 1 :(得分:0)
您可以检查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>