I am currently working on piece of component, I need an if else statement to be done to filter out wether a page object is null or not, here is my attempts:
[#assign page = cmsfn.page(component)]
[#if page IS NULL ] // not working...
[@cms.component content=cmsfn.asContentMap(component) editable=false/]
[/#if]
and this one
[#assign page = cmsfn.page(component)]
[#if !page?has_content ] // not working...
[@cms.component content=cmsfn.asContentMap(component) editable=false/]
[/#if]
What I am trying to do here is, if the page object is null , then do the component rending, these page object are jrc children nodes, when rendering component this type of node mess up thing template, so I need to filter out and make sure the page is null, then render.
Any suggestions? please provide me a code example. Thanks
The template language of FreeMarker (2.x) has this... quirk, that it doesn't have a null
value. Thus, you can't store null
in a variable. When you have foo.bar
where bar
corresponds to Java getBar()
which return null
, then as far as the template language is concerned, foo
simply doesn't contain bar
. And, referring to something that doesn't exist is illegal, unless, you apply a null
/missing handler operator directly on the referring expression (like foo.bar!'myDefault'
or foo.bar??
).
So the simplest approach is to avoid the assignment like [#if cmsfn.page(component)??]...[/#if]
. But sometimes that's not acceptable as then you have to get the page
for a second time further down. Then you can use some default that you can differentiate from the non-default. Assuming that for a page
object ?has_content
gives true
(and unless you are using some strange ObjectWrapper
it does), a default value like {}
(empty hash) suffices. The exp!
operator can be used as a shorthand, as it also gives a default value for which ?has_content
is false:
[#assign page = cmsfn.page(component)!]
[#if page?has_content]
[@cms.component content=cmsfn.asContentMap(component) editable=false/]
... Do something with `page`, otherwise we need not use #assign.
[#else]
... Don't do anything with `page`, it's that strange default object.
[/#if]