简体   繁体   English

PHP:如何将smarty部分用于以下输出?

[英]PHP : How to use smarty section for the following output?

I am using the even and odd logic for that but not getting the output as i need 我为此使用了偶数和奇数逻辑,但没有得到我需要的输出

for example i have array of 例如我有数组

$data = array(1000,1001,1002, 1003,1004,1005);
$smarty->assign('data',$data);

{section name=i loop=$data}

{section}

so the output i need is : 所以我需要的输出是:

<div>
    <dl>1000</dl>
    <dl>1001</dl>
</div>

<div>
    <dl>1002</dl>
    <dl>1003</dl>
</div>

<div>
    <dl>1004</dl>
    <dl>1005</dl>
</div>

According to offical document http://www.smarty.net/docsv2/en/language.function.section.tpl 根据官方文档http://www.smarty.net/docsv2/en/language.function.section.tpl

Try following codes: 尝试以下代码:

<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>

You should do it this way: 您应该这样进行:

{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> should be rather created in section because when there weren't be any items you probably wouldn't like to create empty <div> . 应该在本节中创建<div> ,因为当没有任何项目时,您可能不想创建空的<div> Last condition $smarty.section.i.last is added in case you have for example 5 elements and in that case you of course make sure your div is closed. 最后一个条件$smarty.section.i.last被添加,以防您有5个元素,在这种情况下,您当然要确保div已关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM