如何使用 smarty 在 continue 语句中初始化 for 循环的迭代

how to initialize iteration of for loop in continue statement using smarty

我有这一堆代码,这里我正在获取全年数据,所以在 foreach 我正在检查一个条件;如果年份大于等于当前年份,则使用 continue 语句跳过该年份。

我的问题是在跳过年份时,迭代也会按照跳过的年份跳过。

如果跳过 2 年,那么迭代将从 3 而不是 1 开始。

有什么解决办法吗?如何在 smarty 中重新定义迭代变量。

使用 smarty 模板我没有得到任何关于如何在 else 部分初始化迭代变量的解决方案。

下面我发布了我的代码:

 {foreach from=$histories key=year  item=i name=foo }
    {if $i.year >= date("Y")}
    {continue}
    {else}
   //how to initialise smarty variable here to 1;  
    {/if}
    <tr>
    <td><a href="{$urls.history_details|replace:'%s':$i.year}">{$smarty.foreach.foo.iteration}</a></td>
    <td><a href="{$urls.history_details|replace:'%s':$i.year}">{$i.year}</a></td>
    <td><a href="{$urls.history_details|replace:'%s':$i.year}">{$i.country}</a></td>
    <td><a href="{$urls.history_details|replace:'%s':$i.year}">{$i.location}    </a></td>

    </tr>

    {/foreach}

而不是在条件满足时跳过迭代;您可以以相反的方式继续处理您的数据,如果年份小于当前年份

,您可以继续处理数据

这是您可以做的;

{foreach from=$histories key=year  item=i name=foo }
    {if $i.year < date("Y")}
        {* Your code with row details here *}
    {/if}
{/foreach}