I think this solves the multiple lists problem (this works for a long
time):

  <#list mylist1 + list2 as item>


Monday, June 13, 2016, 10:26:17 AM, Albert Kam wrote:

> Dear Daniel,
>
> I think you nailed it, it's obviously a filtering problem !
>
> One more issue regarding json comma crossed my head just now.
> Example being this:
> {
> <#list mylist1 as item>
>    '${item}': '${item}'<#sep>,</#sep>
> </#list>,
> <#list mylist2 as item>
>    '${item}': '${item}'<#sep>,</#sep>
> </#list>
> }
>
> If mylist1 is empty, then it will result in
> {
>   ,
>   'mylist2item1': 'mylist2item1',
>   'mylist2item2': 'mylist2item2'
> }
> And if mylist2 happens to be empty, then it'll result in
> {
>   'mylist2item1': 'mylist2item1',
>   'mylist2item2': 'mylist2item2'
>   ,
> }
> If both are empty, then
> {
>   ,
> }
>
> So the ugly and not reusable way to deal with this would be:
> {
> <#list mylist1 as item>
>    '${item}': '${item}'<#sep>,</#sep>
> </#list><#if mylist1?has_content && mylist2?has_content>,</#if>
> <#list mylist2 as item>
>    '${item}': '${item}'<#sep>,</#sep>
> </#list>
> }
>
> The reusable approach is maybe sth like this
> {
> <@optionalNested commaSuffix=true>
>   <#list mylist1 as item>
>      '${item}': '${item}'<#sep>,</#sep>
>   </#list>
> </@optionalNested>
> <#list mylist2 as item>
>    '${item}': '${item}'<#sep>,</#sep>
> </#list>
> }
> where optional nested only adds a comma at the end when it's
> trimmed-<#nested> is not empty.
>
> Are there any other solutions that i might miss ?
>
>
>
> On Mon, Jun 13, 2016 at 1:55 PM, Daniel Dekany <[email protected]> wrote:
>> That's a work around for the separator, but this problem is deeper
>> that that. Things like x?index, x?hasNext, x?itemParity, etc. don't
>> work either if you do filtering like that. So I plan to add closure
>> support (if it's technically possible...) and then you can do
>> something like:
>>
>>   <#list hobbies?filter(i -> i != 'walk') as hobby>
>>
>> Though you meant to filter lists before passing them to the
>> data-model, so it will also support bad practices... But I know that
>> in real world filtering has good uses too.
>>
>>
>> Monday, June 13, 2016, 3:55:29 AM, Albert Kam wrote:
>>
>>> Hello Daniel,
>>>
>>> Thanks for the great updates !
>>>
>>> I have something in mind concerning the <#sep> directive. Is it
>>> possible to add a startSeparator only when the previous loop produces
>>> content ?
>>> This happens a lot when printing json hash entries. Example :
>>>
>>> <#local hobbies=['read', 'code', 'walk']>
>>> {
>>> <#list hobbies as hobby>
>>>     <#if hobby != 'walk>
>>>         'prop${hobby_index}': '${hobby}'<#sep>,<#sep>
>>>     </#if>
>>> </#list>
>>> }
>>>
>>> This will result in
>>> {
>>>     'prop0': 'read',
>>>     'prop1': 'walk',
>>> }
>>> Notice the extra comma after 'walk'.
>>>
>>> I have a workaround for this issue with adding the comma at the head
>>> if already had an entry:
>>> <#local hasEntry=false>
>>> <#list hobbies as hobby>
>>>     <#if hobby != 'walk>
>>>         <#if hasEntry>,</#if>
>>>         'prop${hobby_index}': '${hobby}'
>>>         <#local hasEntry=true>
>>>     </#if>
>>> </#list>
>>>
>>> This will result in
>>> {
>>>     'prop0': 'read'
>>>     ,
>>>     'prop1': 'walk'
>>> }
>>>
>>> The ideal directive to emultate the workaround above would look
>>> something like this:
>>> <#list hobbies as hobby>
>>>     <#if hobby != 'walk>
>>>         <#head_sep>,<#head_sep>
>>>         'prop${hobby_index}': '${hobby}'
>>>     </#if>
>>> </#list>
>>> Where <#head_sep> renderes the comma only if the previous loop had an entry.
>>>
>>> Just a suggestion.
>>>
>>> Thanks !
>>>
>>>
>>> On Mon, Jun 13, 2016 at 3:32 AM, Daniel Dekany <[email protected]> wrote:
>>>> I plan to start a voting here on 2016-06-14, for releasing 2.3.25. So
>>>> if anyone wants to add anything before that, inform me.
>>>>
>>>> This is what we have so far:
>>>> - Version history: 
>>>> http://freemarker.org/builds/2.3.25-nightly/documentation/_html/versions_2_3_25.html
>>>> - Last build: http://freemarker.org/builds/2.3.25-nightly/freemarker.jar
>>>>
>>>> --
>>>> Thanks,
>>>>  Daniel Dekany
>>>>
>>>
>>>
>>>
>>
>> --
>> Thanks,
>>  Daniel Dekany
>>
>
>
>

-- 
Thanks,
 Daniel Dekany

Reply via email to