[MAGNOLIA-6373] Update Freemarker from '2.3.21' to '2.3.25': Many powerful improvements Created: 15/Sep/15  Updated: 09/Feb/17  Resolved: 14/Jul/16

Status: Closed
Project: Magnolia
Component/s: templating
Affects Version/s: 5.4.2
Fix Version/s: 5.5

Type: Improvement Priority: Neutral
Reporter: Christian Ringele Assignee: Maxime Michel
Resolution: Fixed Votes: 8
Labels: pm, support, templating
Remaining Estimate: 0d
Time Spent: 5m
Original Estimate: Not Specified

Issue Links:
Relates
relates to MGNLDEMO-103 Demo advanced form including Multi-Step Closed
causality
dependency
depends upon MAGNOLIA-6814 Update 3rd-party libraries for next m... Closed
Template:
Acceptance criteria:
Empty
Task DoD:
[ ]* Doc/release notes changes? Comment present?
[ ]* Downstream builds green?
[ ]* Solution information and context easily available?
[ ]* Tests
[ ]* FixVersion filled and not yet released
[ ]  Architecture Decision Record (ADR)
Release notes required:
Yes
Date of First Response:
Epic Link: Light Development 1.0
Sprint: Basel 48, Basel 52
Story Points: 2

 Description   

Reading the freemarker documentation lately again, I found many very powerful improvements which were added in the 2.3.23 version.

Probably the most powerful improvement is the list iteration:
http://freemarker.org/docs/ref_directive_list.html

The new possibility of only looping when the list is not null is really great and saves a lot of if statements:

<#list sequence>
    Part executed once if we have more than 0 items
    <#items as item>
        Part repeated for each item
    </#items>
    Part executed once if we have more than 0 items
<#else>
    Part executed when there are 0 items
</#list>

Here a typical 'non emtpy ul" example (also typical cases would be: divs and their classes):
Freemarker version < 2.3.23 (old):

[#if (components?size) > 0]
<ul>
    [#list components as component ]
        <li>[@cms.component content=component /]</li>
    [/#list]
</ul>
[#else]
Do something on an empty list
[/#if]

Freemarker version = 2.3.23 (new):

[#list components]
<ul>
    [#items as component]
        <li>[@cms.component content=component /]</li>
    [/#items]
</ul>
[#else]
Do something on an empty list
[/#list]

And here a typical example of more complex logic. Its from a simple navigation suing recursive macro:
Freemarker version < 2.3.23 (old):

[#assign rootPage = cmsfn.siteRoot(content, "training-home") /]
[@linkChildren node=rootPage /]

[#macro linkChildren node ulClass="nav navbar-nav"]
    [#assign children = cmsfn.children(child, "mgnl:page") /]
    [#if (children?size) > 0]
        <ul class="${ulClass}">
            [#list children as child]
            
                [#assign has2ndLevelChildren = (cmsfn.children(child, "mgnl:page")?size) > 0 /]
                [#if has2ndLevelChildren]
                    <li class="dropdown">
                        <a href="${cmsfn.link(child)!}" > ${child.title!child.@name}</a>
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false" >
                            <span class="caret"></span>
                        </a>
                        [@linkChildren node=child ulClass="dropdown-menu"/]
                    </li>
                [#else]
                    <li><a href="${cmsfn.link(child)!}">${child.title!child.@name}</a></li>        
                [/#if]      
                      
            [/#list]
        </ul>
    [/#if]    
[/#macro]

Freemarker version = 2.3.23 (new):

[#assign rootPage = cmsfn.siteRoot(content, "training-home") /]
[@linkChildren node=rootPage /]

[#macro linkChildren node ulClass="nav navbar-nav"]
    [#assign children = cmsfn.children(child, "mgnl:page") /]
    [#list children]
        <ul class="${ulClass}">
            [#items as child]
    
                [#list cmsfn.children(child, "mgnl:page")]
                    <li class="dropdown">
                        <a href="${cmsfn.link(child)!}" > ${child.title!child.@name}</a>
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false" >
                            <span class="caret"></span>
                        </a>
                        [@linkChildren node=child ulClass="dropdown-menu"/]
                    </li>
                [#else]
                    <li><a href="${cmsfn.link(child)!}">${child.title!child.@name}</a></li> 
                [/#list]  
                  
            [/#items]         
        </ul>
    [/#list]
[/#macro]

I hope the benefit is well view-able.



 Comments   
Comment by Jan Haderka [ 07/Jul/16 ]

Actually, there is even newer release. And while normally I would wait for a while, this release fixed issue in TDL support introduced in .23 to which we have just upgraded ( https://issues.apache.org/jira/browse/FREEMARKER-18 ) and also fixes issue with merging of custom date and number formats. Plus it adds support for direct iteration of map entries, lazy loading of includes.

Generated at Mon Feb 12 04:13:55 CET 2024 using Jira 9.4.2#940002-sha1:46d1a51de284217efdcb32434eab47a99af2938b.