-
Improvement
-
Resolution: Fixed
-
Neutral
-
5.4.2
-
-
Empty show more show less
-
Yes
-
Basel 48, Basel 52
-
2
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.
- depends upon
-
MAGNOLIA-6814 Update 3rd-party libraries for next major 5.5 release
- Closed
- relates to
-
MGNLDEMO-103 Demo advanced form including Multi-Step
- Closed