freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ondra Žižka (JIRA) <j...@apache.org>
Subject [jira] [Commented] (FREEMARKER-37) ability to skip iteration in a list
Date Mon, 02 Oct 2017 05:06:00 GMT

    [ https://issues.apache.org/jira/browse/FREEMARKER-37?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16187662#comment-16187662
] 

Ondra Žižka commented on FREEMARKER-37:
---------------------------------------

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)

Mime
View raw message