You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@freemarker.apache.org by Albert Kam <mo...@gmail.com> on 2016/05/22 09:47:22 UTC

A cautionary case for <#sep>

Hi there, just want to share a cautionary case that i faced just now.

<#if loopable_has_next>, </#if>
into
<#sep>,

But bad story for me as i didnt realize <#sep>, skips all the logic
below it on the last loop as can be seen from this simple code:

<@testSep />

<#macro testSep>
    <#list 1..1 as num>
        number ${num}<#sep>,
        -- This is not evaluated at the last loop --
        -- The logic below is not evaluated also --
        -- ${num * 10}
    </#list>
</#macro>

which results in "number 1" only.

So, never just replace the old _has_next to <#sep> blindly like i did, ;-)

Perhaps cases like this could be prevented by something like <#sep ', '> ?

Anyway, a good new directive, i hated having to do _has_next all the
time also, but just want to share this case i faced to others.

-- 
Do not pursue the past. Do not lose yourself in the future.
The past no longer is. The future has not yet come.
Looking deeply at life as it is in the very here and now,
the practitioner dwells in stability and freedom.
(Thich Nhat Hanh)

Re: A cautionary case for <#sep>

Posted by Daniel Dekany <dd...@freemail.hu>.
The most generic replacement for

  <#if loopable_has_next>, </#if>

is

  <#sep>, </#sep>

and not just

  <#sep>,

If you omit </#sep>, like you did, then it's implied directly before
the </#list>. Since you had statements after that </#if>, use an
explicit </#sep> where the </#if> was.

-- 
Thanks,
 Daniel Dekany


Sunday, May 22, 2016, 11:47:22 AM, Albert Kam wrote:

> Hi there, just want to share a cautionary case that i faced just now.
>
> <#if loopable_has_next>, </#if>
> into
> <#sep>,
>
> But bad story for me as i didnt realize <#sep>, skips all the logic
> below it on the last loop as can be seen from this simple code:
>
> <@testSep />
>
> <#macro testSep>
>     <#list 1..1 as num>
>         number ${num}<#sep>,
>         -- This is not evaluated at the last loop --
>         -- The logic below is not evaluated also --
>         -- ${num * 10}
>     </#list>
> </#macro>
>
> which results in "number 1" only.
>
> So, never just replace the old _has_next to <#sep> blindly like i did, ;-)
>
> Perhaps cases like this could be prevented by something like <#sep ', '> ?
>
> Anyway, a good new directive, i hated having to do _has_next all the
> time also, but just want to share this case i faced to others.
>