0

我有生成的循环:

< li > 1 2 3 < / li > 4 5 6 < li > 7 8 9 < / li > 10 11 12 < li > 13 14 15 < / li >

但我的问题是,有时循环最终会产生较少的结果。最后,总是需要有</li>。所以它可以看起来像这样,

< li > 1 2 3 < / li > 4 5 6 < li > 7 8 < / li >

或者

< li > 1 2 3 < / li > 4 5 6 < li > 7 < / li >

我想出了这样的代码,但它不能正常工作,

{section name=attribs loop=14}

{assign var=zamkniecie value=''}

{if $smarty.section.attribs.iteration % 5 == 0 or $smarty.section.attribs.iteration == 1}
        < li > {$smarty.section.attribs.iteration}
{/if}

{if $smarty.section.attribs.iteration % 4 == 0}
        < / li > {$smarty.section.attribs.iteration}
        {assign var=zamkniecie value=$smarty.section.attribs.iteration}
{/if}

{/section}

{if !$zamkniecie} < / li > {/if}

谢谢,

4

1 回答 1

0

您可以使用.first代替.iteration == 1, and.last来发现循环中的最后一次运行。.index对于这样的数学,使用从 开始的0,而不是从.iteration开始的 ,也更有用1。您还想检查% 3两个 if 语句 - 在您的示例中,一个是 5 个组,另一个是 4 个组!

我认为这应该有效:

{section name=attribs loop=14}

{* Open an item if this is the first entry (#0), or any that is divisible by 3 (#3, #6, etc) *}
{* Note that the .first is actually redundant, because 0 % 3 == 0 anyway *}
{if $smarty.section.attribs.index % 3 == 0 or $smarty.section.attribs.first}
        <li>
{/if}

{* Output the current item *}
{$smarty.section.attribs.iteration}

{* Close the current item if this is the last entry, or is the end of a group of 3 *}
{* The test is for indexes which leave a remainder of 2 when divided by 3: #2, #5, etc *}
{if $smarty.section.attribs.index % 3 == 2 or $smarty.section.attribs.last}
        </li>
{/if}

{/section}
于 2013-10-02T18:57:33.473 回答