Message Boards

Helpful Hints: Comparing Dates In Freemarker

Lee Jordan, modified 3 Years ago.

Helpful Hints: Comparing Dates In Freemarker

Expert Posts: 449 Join Date: 5/26/15 Recent Posts
Heads up on comparing dates in Freemarker for web content. I've found a bug whereby sometimes if you have a web content structure and template situation and you have hundreds of already published articles with that structure ... if you add a new structure and try to diplsay it all the exosting web content breaks.


Compare dates using ?long

[code]<!-- Template updated 8/13/2020 or 1597352716000 -->
&lt;#assign changed = 1597352716000&gt;
&lt;#assign date = .vars['reserved-article-modified-date'].data&gt;
&lt;#assign date = date?datetime("EEE, d MMM yyyy HH:mm:ss Z")&gt;
&lt;#assign date = date?long&gt;

&lt;#assign now = .now?long&gt;

<p>1: ${date}</p>
<p>2: ${changed}</p>
<p>3: ${now}</p>
    

&lt;#if date lt changed&gt;
    &lt;#assign cstatus = "stale"&gt;
&lt;#elseif date gt changed&gt;
    &lt;#assign cstatus = "fresh"&gt;
&lt;#else&gt;
    &lt;#assign cstatus = "unknown"&gt;
<!--#if-->

<p>Content Freshness: ${cstatus}</p>
It is possible to use ?? in Freemarker but for some reason this doesn't always suppress the "missing reference errors". The template is refrencing a content field that isn't in the old versions of the structures. The error goes away when the user republishes the content but this isn't acceotable to re-publish hundreds of articles.


I had to insert a hard epoch date as a cut off to judge if the content was updated after the change, if so it will find the missing data, if not it can't find the missing data ... so only look for the data if the content is freshly published.

This may be useful to someone.
thumbnail
Christoph Rabel, modified 3 Years ago.

RE: Helpful Hints: Comparing Dates In Freemarker

Liferay Legend Posts: 1554 Join Date: 9/24/09 Recent Posts
You need to check for the field and also check the return of the getData method.
<#if newField?? && newField.getData()??> ...
That should work for new fields. We add fields all the time.
It is not really a bug, it is more like how this thing works. It reads the content and what's in there, gets added. It doesn't look at the structure and checks what fields could be available. That's why it works after saving the content, the empty field is then added to the content.
I have opened two feature requests a while ago regarding article fields:
https://issues.liferay.com/browse/LPS-114690
https://issues.liferay.com/browse/LPS-114686