This website uses cookies to ensure you get the best experience. Learn More.
Migrating Web content templates from Velocity to Freemarker: tools, tips and tricks
This blog post contains a simple procedure to upgrade the templates created for Web Content in Liferay Portal 6.x EE with Velocity to Freemarker in Liferay Portal/DXP 7.x. As you might know, the use of Velocity is deprecated in Liferay Portal/DXP 7.x and it’s advisable to migrate from Velocity to Freemarker.
This post is not magic or rocket science, just a set of steps that can help in automatization of some steps that would otherwise be manual.
Let’s start reviewing the tools used and then the tips and tricks within a complete procedure.
Sublime Text is the Text editor used.
It can be installed in Sublime Text following Preferences > Package Control > Install and write HTMLBeautify.
<#if
<#foreach
</#if
</#foreach
<#elseif
<#else
Below is a picture of the final configuration:
Obviously, Liferay Portal or DXP 7.x after its database has been upgraded.
Follow this procedure with one of your Velocity templates for Web Content to test if it’s useful for you:
In order to copy the code inside the templates, it can be very useful to load them using WebDAV. For that, open a remote connection using the File Explorer in your OS to one URL like this one: http://localhost:8080/webdav/guest/journal/Templates/ . In the URL, remember to change:
Instead of “localhost:8080”, the domain of your Liferay server where the templates are hosted.
Instead of “guest”, the name of the Website where the templates are stored.
Once the templated directory is mounted through WebDAV, copy the files to the working directory where you unzipped USCavalry.
Add an extension to those files, such as “.vm”.
In the working directory, run the following command with one of the existing files as the first argument and a new file as the second argument: java -jar cavalry.jar 11843.vm 11843.ftl
java -jar cavalry.jar 11843.vm 11843.ftl
After the previous step is completed, you will see that the output of the Freemarker file (the .ftl one), is unindented making the readability of that file very difficult. In order to tidy up that file, follow these steps:
First, separate every HTML or Freemarker tag into a different line. To achieve this, in Sublime Text replace the closing tag character (>) with the same character followed by the new line escape sequence (>\n), as you can see in the following image:
>
>\n
Second, apply HTMLBeautify. In Sublime Text, go to Edit > Beautify HTML (if it doesn’t work, go to line 1 and run it again)
At this stage, the code in the templates would be correctly indented and formatted to allow easy reading.
Lastly, you will need to apply additional adaptations to your template, such as:
<#if someVariable && someVariable.getData() != "">
<#if someVariable?? && someVariable.getData() != "">
<#assign journalArticleLocalService = serviceLocator.findService("com.liferay.portlet.journal.service.JournalArticleLocalService")>
<#assign journalArticleLocalService = serviceLocator.findService("com.liferay.journal.service.JournalArticleLocalService")>
com.liferay.portlet.asset.service.AssetEntryLocalService
com.liferay.asset.kernel.service.AssetEntryLocalService
com.liferay.portlet.asset.service.AssetCategoryLocalService
com.liferay.asset.kernel.service.AssetCategoryLocalService
com.liferay.portlet.asset.service.AssetVocabularyLocalService
com.liferay.asset.kernel.service.AssetVocabularyLocalService
<#assign ja = journalArticleLocalService.getArticle(getterUtil.getLong(groupId), getterUtil.getString(reserved-article-id.data))>
<#assign ja = journalArticleLocalService.getArticle(getterUtil.getLong(groupId), getterUtil.getString(.vars['reserved-article-id'].data))>
Keep in mind that other data fields are available. See the following class for a complete reference.
<#if !repeatableField.getSiblings().isEmpty()>
<#if repeatableField.getSiblings()?has_content>
VARIABLE
TYPE
accountPermission
com.liferay.portal.kernel.service.permission.AccountPermission
arrayUtil
com.liferay.portal.kernel.util.ArrayUtil_IW
auditMessageFactoryUtil
com.liferay.portal.kernel.audit.AuditMessageFactory
bodyCssClass
java.lang.String
browserSniffer
com.liferay.portal.kernel.servlet.BrowserSniffer
calendarFactory
com.liferay.portal.kernel.util.CalendarFactory
colorScheme
com.liferay.portal.kernel.model.ColorScheme
commonPermission
com.liferay.portal.kernel.service.permission.CommonPermission
company
com.liferay.portal.kernel.model.Company
dateFormatFactory
com.liferay.portal.kernel.util.FastDateFormatFactory
dateFormats
dateUtil
com.liferay.portal.kernel.util.DateUtil_IW
enumUtil
freemarker.ext.beans.BeansWrapper
expandoColumnLocalService
com.liferay.expando.kernel.service.ExpandoColumnLocalService
expandoRowLocalService
com.liferay.expando.kernel.service.ExpandoRowLocalService
expandoTableLocalService
com.liferay.expando.kernel.service.ExpandoTableLocalService
expandoValueLocalService
com.liferay.expando.kernel.service.ExpandoValueLocalService
freeMarkerPortletPreferences
com.liferay.portal.template.TemplatePortletPreferences
fullCssPath
fullTemplatesPath
getterUtil
com.liferay.portal.kernel.util.GetterUtil_IW
groupPermission
com.liferay.portal.kernel.service.permission.GroupPermission
htmlUtil
com.liferay.portal.kernel.util.Html
httpUtil
com.liferay.portal.kernel.util.Http
httpUtilUnsafe
imageToken
com.liferay.portal.kernel.webserver.WebServerServletToken
imageToolUtil
com.liferay.portal.kernel.image.ImageTool
init
jsonFactoryUtil
com.liferay.portal.kernel.json.JSONFactory
languageUtil
com.liferay.portal.kernel.language.Language
layout
com.liferay.portal.kernel.model.Layout
layoutPermission
com.liferay.portal.kernel.service.permission.LayoutPermission
layouts
java.util.List
layoutTypePortlet
com.liferay.portal.kernel.model.LayoutTypePortlet
locale
java.util.Locale
localeUtil
com.liferay.portal.kernel.util.LocaleUtil
locationPermission
com.liferay.portal.kernel.service.permission.OrganizationPermission
navItems
objectUtil
freemarker.template.TemplateMethodModelEx
organizationPermission
pageTitle
paramUtil
com.liferay.portal.kernel.util.ParamUtil_IW
passwordPolicyPermission
com.liferay.portal.kernel.service.permission.PasswordPolicyPermission
permissionChecker
com.liferay.portal.kernel.security.permission.PermissionChecker
plid
portal
com.liferay.portal.kernel.util.Portal
portalPermission
com.liferay.portal.kernel.service.permission.PortalPermission
portalUtil
portletDisplay
com.liferay.portal.kernel.theme.PortletDisplay
portletGroupId
java.lang.Long
portletModeFactory
com.liferay.portal.kernel.portlet.PortletModeFactory_IW
portletPermission
com.liferay.portal.kernel.service.permission.PortletPermission
portletProviderAction
java.util.Map
portletURLFactory
com.liferay.portal.kernel.portlet.PortletURLFactory
prefsPropsUtil
com.liferay.portal.kernel.util.PrefsProps
propsUtil
com.liferay.portal.kernel.util.Props
randomizer
com.liferay.portal.kernel.util.Randomizer
realUser
com.liferay.portal.kernel.model.User
request
javax.servlet.http.HttpServletRequestWrapper
rolePermission
com.liferay.portal.kernel.service.permission.RolePermission
saxReaderUtil
com.liferay.portal.kernel.xml.SAXReader
scopeGroupId
serviceLocator
com.liferay.portal.template.ServiceLocator
sessionClicks
com.liferay.portal.kernel.util.SessionClicks_IW
xmlRequest
java.lang.Object
staticFieldGetter
com.liferay.portal.kernel.util.StaticFieldGetter
staticUtil
freemarker.ext.beans.StaticModels
stringUtil
com.liferay.portal.kernel.util.StringUtil_IW
theme
com.liferay.portal.kernel.model.Theme
themeDisplay
com.liferay.portal.kernel.theme.ThemeDisplay
timeZone
java.util.TimeZone
timeZoneUtil
com.liferay.portal.kernel.util.TimeZoneUtil_IW
unicodeFormatter
com.liferay.portal.kernel.util.UnicodeFormatter_IW
unicodeLanguageUtil
com.liferay.portal.kernel.language.UnicodeLanguage
user
userGroupPermission
com.liferay.portal.kernel.service.permission.UserGroupPermission
userPermission
com.liferay.portal.kernel.service.permission.UserPermission
utilLocator
com.liferay.portal.template.UtilLocator
validator
com.liferay.portal.kernel.util.Validator_IW
webServerToken
I hope this blog post was useful for you and it can save you some time in the task of migrating off your Web content templates from Velocity to Freemarker.
The combination of the migration tool (USCavalry) and a Text editor that can tidy up your template will make all that process easier.
Simply marvelous