[英]PHP : How to use smarty section for the following output?
我為此使用了偶數和奇數邏輯,但沒有得到我需要的輸出
例如我有數組
$data = array(1000,1001,1002, 1003,1004,1005);
$smarty->assign('data',$data);
{section name=i loop=$data}
{section}
所以我需要的輸出是:
<div>
<dl>1000</dl>
<dl>1001</dl>
</div>
<div>
<dl>1002</dl>
<dl>1003</dl>
</div>
<div>
<dl>1004</dl>
<dl>1005</dl>
</div>
根據官方文檔http://www.smarty.net/docsv2/en/language.function.section.tpl
嘗試以下代碼:
<div>
{section name=i loop=$data}
<dl>{$data[i]}</dl>
{if $smarty.section.data.index > 0 && $smarty.section.data.index % 2 == 0 && $smarty.section.data.index < $smarty.section.customer.total -1}
</div><div>
{/if}
{/section}
</div>
您應該這樣進行:
{section name=i loop=$data}
{if $smarty.section.i.index %2 == 0}
<div>
{/if}
<dl>{$data[i]}</dl>
{if $smarty.section.i.index %2 == 1 || $smarty.section.i.last}
</div>
{/if}
{/section}
應該在本節中創建<div>
,因為當沒有任何項目時,您可能不想創建空的<div>
。 最后一個條件$smarty.section.i.last
被添加,以防您有5個元素,在這種情況下,您當然要確保div已關閉。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.