Uploaded image for project: 'Magnolia'
  1. Magnolia
  2. MAGNOLIA-6373

Update Freemarker from '2.3.21' to '2.3.25': Many powerful improvements

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Neutral
    • Resolution: Fixed
    • Affects Version/s: 5.4.2
    • Fix Version/s: 5.5
    • Component/s: templating
    • Release notes required:
      Yes
    • Sprint:
      Basel 48, Basel 52
    • Story Points:
      2
    • Magnolia Release:
      5.5

      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.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                mmichel Maxime Michel
                Reporter:
                cringele Christian Ringele
              • Votes:
                8 Vote for this issue
                Watchers:
                9 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Date of First Response:

                  Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0d
                  0d
                  Logged:
                  Time Spent - 5m
                  5m