douxu5233
2014-07-03 05:09
浏览 27
已采纳

PHP:如何使用smarty部分进行以下输出?

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>
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dousao6260 2014-07-03 06:00
    已采纳

    According to offical document 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>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douke6424 2014-07-03 07:15

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

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题