How to handle a structure field modification in templates
How To articles are not official guidelines or officially supported documentation. They are community-contributed content and may not always reflect the latest updates to Liferay DXP. We welcome your feedback to improve How To articles!
While we make every effort to ensure this Knowledge Base is accurate, it may not always reflect the most recent updates or official guidelines.We appreciate your understanding and encourage you to reach out with any feedback or concerns.
Legacy Article
You are viewing an article from our legacy "FastTrack"
publication program, made available for informational purposes. Articles
in this program were published without a requirement for independent
editing or verification and are provided"as is" without
guarantee.
Before using any information from this article, independently verify its
suitability for your situation and project.
Issue
- The user changed a web content structure and moved a nested field to the top level of this structure.
- It produces an error when some articles are rendered.
- Let's provide an example:
-
- We create an structure with 2 fields:
- parent
- child (that is nested in parent)
- A new Template is created with this code:
<p>Parent:${parent.getData()}</p>
<p>Child:${parent.child.getData()}</p>
- A new article (named ARTICLE-1) is created at this point.
- The child field is moved to the root of the structure. Now we have 2 sibling fields:
- A new article (named ARTICLE-2) is created at this point.
- We add 2 web content display portlets in a page, and select both articles (one in each portlet).
- Result:
- ARTICLE-1 is rendered as expected (because the template handles the field names as it needs)
- ARTICLE-2 produces an error:
2022-05-10 08:26:37.549 ERROR [http-nio-8080-exec-6][runtime:59] Error executing FreeMarker template
FreeMarker template error:
The following has evaluated to null or missing:
==> parent.child [in template "20101#20128#35258" at line 10, column 12]
----
Tip: It's the step after the last dot that caused this error, not those before it.
----
Tip: If the failing expression is known to legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??
----
----
FTL stack trace ("~" means nesting-related):
- Failed at: ${parent.child.getData()} [in template "20101#20128#35258" at line 10, column 10]
----
Java stack trace (for programmers):
----
Environment
Resolution
- The root cause of the error is that changing the structure does not update its articles' content and the field names are different.
- It means that to render it properly we have to handle both cases in the templates:
-
To solve the issue we have 2 possible solutions:
- Create 2 templates to handle each case.
- Handle this in the same template:
<p>Parent:${parent.getData()}</p>
//original field
<p>
<#if parent.child?? && parent.child.getData()?? >
Nested child field value: ${parent.child.getData()}
<#else>
parent.child value is null
</#if>
</p>
// modifided-moved field
<p>
<#if child?? && child.getData()?? >
Modified child filed value: ${child.getData()}
<#else>
child value is null
</#if>
</p>
Did this article resolve your issue ?