You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@freemarker.apache.org by "Ondra Žižka (JIRA)" <ji...@apache.org> on 2017/10/02 05:10:00 UTC
[jira] [Comment Edited] (FREEMARKER-37) ability to skip iteration
in a list
[ https://issues.apache.org/jira/browse/FREEMARKER-37?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16187662#comment-16187662 ]
Ondra Žižka edited comment on FREEMARKER-37 at 10/2/17 5:09 AM:
----------------------------------------------------------------
I agree that just having {{<#continue>}} might lead to an unexpected/undefined behavior with things like counters, separators etc.
How about having {{<#continue>}} and {{<#skip>}} ? {{<#continue>}} would count as item used, {{<#skip>}} would not. This would pass the responsibility to decide whether to increment the counter or not to the user. It makes sense, because the user knows whether he uses {{is_even_item}} for coloring table rows, and whether that was already taken care of (then they would use {{<#continue>}}) or not (then he would use {{<#skip>}}).
I.e.
{code}
<#list 10..15 as num>
<#if num == 10>
<#continue>
</#if>
<#if num == 11>
<#skip>
</#if>
[${num.index}] == ${num}, ${num.count} items passed, is_first = ${num.is_first?c}
</#list>
{code}
This would print
2 == 12, 1 items passed, is_first=false (I hope I computed right :)
...
Because the {{.counter}} would increment only if the loop is finished or continued, {{is_first}} would derive from the {{counter == 0}}.
Similarly, {{is_even_item}} and {{is_odd_item}} would check the counter rather than index.
{{is_last}} would probably have to rely on {{index}}.
Similarly, separators could appear if {{couter > 0 and index < size-1}}, and, well, the user would have to have some common sense not to expect the separator appear if he uses {{<#skip>}} before {{<#sep>}}.
was (Author: pekarna):
I agree that just having {{<#continue>}} might lead to an unexpected/undefined behavior with things like counters, separators etc.
How about having {{<#continue>}} and {{<#skip>}} ? {{<#continue>}} would count as item used, {{<#skip>}} would not. I.e.
{code}
<#list 10..15 as num>
<#if num == 10>
<#continue>
</#if>
<#if num == 11>
<#skip>
</#if>
[${num.index}] == ${num}, ${num.count} items passed, is_first = ${num.is_first?c}
</#list>
{code}
This would print
2 == 12, 1 items passed, is_first=false
...
Because the {{.counter}} would increment only if the loop is finished or continued, {{is_first}} would derive from the {{counter == 0}}. Similarly, {{is_even_item}} and {{is_odd_item}} would check the counter rather than index. {{is_last}} would probably have to rely on {{index}}. Similarly, separators could appear if {{couter > 0 and index < size-1}}, and, well, the user would have to have some common sense not to expect the separator appear if he uses {{<#skip>}} before {{<#sep>}}.
> ability to skip iteration in a list
> -----------------------------------
>
> Key: FREEMARKER-37
> URL: https://issues.apache.org/jira/browse/FREEMARKER-37
> Project: Apache Freemarker
> Issue Type: Improvement
> Reporter: Ivan Levchenko
>
> Sorely missing ability to skip through an iteration of a list and continue on with the rest of the list. would like to write clean code like this:
> <#list collection as element>
> <#if element.attr1 == "some_value">
> <#continue /> <#-- skip over this item and run next item in loop -->
> </#if>
> do stuff here
> </#list>
> Thanks!
--
This message was sent by Atlassian JIRA
(v6.4.14#64029)