Date objects in Liferay Freemarker web content templates
Have you ever tried to manipulate one of the web content meta data dates as an actual date in a Liferay Freemarker web content template? Unfortunately these dates are handled very poorly by Liferay.
They are returned as a formatted string, based on the default locale of the portal, unless you have manually changed the default locale of the structure the template uses. This means that the only way of obtaining the article create date, article publish date and last updated as actual date objects requires parsing strings, which in itself is a pain. Furthermore, since the date is formatted based on the used locale, you can't simply just parse the date either. Now in velocity you have access to a nice date utility that can parse a date string based on a provided locale. Unfortunately no such feature exists in Freemarker, so you have to change the actual locale of the template back and forth. Finally, not that this will only work if the default language of the structure the template is based on is the same as the default language of the portal. If they differ, your are simply out of luck as far as I have found out. If anybody knows how to solve this if these default locales differ, please, please let me know.
<#-- Retrieve the published date meta data field of the web content -->
<#assign displaydate = .vars['reserved-article-display-date'].data>
<#-- Save the original page locale for later -->
<#assign originalLocale = .locale>
<#-- Set the page locale to the portals default locale -->
<#setting locale = localeUtil.getDefault()>
<#-- Parse the date to a date object -->
<#assign displaydate = displaydate?datetime("EEE, d MMM yyyy HH:mm:ss Z")>
<#-- Set the page locale back to the original page locale -->
<#assign locale = originalLocale>
Now you can use the date as a normal date object in freemarker:
${displaydate?string["yyyy-MM-dd"]}
Other meta data dates
The following three meta data dates are available on web content in Liferay:
reserved-article-display-date
reserved-article-create-date
reserved-article-modified-date
So to use all three of theme, you could use the following code:
<#-- Retrieve the published date meta data field of the web content -->
<#assign displaydate = .vars['reserved-article-display-date'].data>
<#assign createdate = .vars['reserved-article-create-date'].data>
<#assign modifieddate = .vars['reserved-article-modified-date'].data>
<#-- Save the original page locale for later -->
<#assign originalLocale = .locale>
<#-- Set the page locale to the portals default locale -->
<#setting locale = localeUtil.getDefault()>
<#-- Parse the date to a date object -->
<#assign displaydate = displaydate?datetime("EEE, d MMM yyyy HH:mm:ss Z")>
<#assign createdate = createdate?datetime("EEE, d MMM yyyy HH:mm:ss Z")>
<#assign modifieddate = modifieddate?datetime("EEE, d MMM yyyy HH:mm:ss Z")>
<#-- Set the page locale back to the original page locale -->
<#assign locale = originalLocale>
And you would use them all as normal, for example:
${displaydate?date}
${createdate?time}
${modifieddate?iso_utc}
Photo by Sonja Langford / Unsplash