SamWM
SamWM

Reputation: 5356

How do you check the Liferay version in a FreeMarker template?

I am working on a theme for Liferay that I wish to work with both versions 7.2 and 7.3 of Liferay. How can I check what version is being used within the template, so I can do code like this:

    <#if liferay_version == "7.3">
      <p>Liferay 7.3</p>
    <#elseif liferay_version == "7.2">
      <p>Liferay 7.2</p>
    </#if>

Update (21/08):
I have tried the using assign to get the service (as set can't be used in the template), but serviceLocator is null. <#assign releaseLocalService = serviceLocator.findService("com.liferay.portal.service.ReleaseLocalService")>

Upvotes: 1

Views: 1900

Answers (2)

orithena
orithena

Reputation: 1485

Pratiks answer should work if you port it from Velocity to Freemarker and remove serviceLocator from the set of restricted variables (Control Panel -> Configuration -> System Settings -> Template Engines -> Freemarker):

<#assign 
  releaseLocalService = serviceLocator.findService("com.liferay.portal.service.ReleaseLocalService")
  release = releaseLocalService.getRelease(1)
  vers = release.getBuildNumber()
/>
<span class="simpleVersion">${vers}</span>

If you don't want to go the route of allowing anyone with the permissions of editing templates on your Liferay instance to basically load any service and therefore any data on the server (because if you make the serviceLocator unrestricted, it will be available even in Page Fragment Templates)... well, then you will need to inject the variables you need using a TemplateContextContributor: Simply create a new Liferay Module, selecting 'template-context-contributor' as Project Template to derive from, then edit its main class to follow the code below. Also, you'd need to write the MyHelper class yourself (in the same module) and annotate that one with @Component(immediate = true, service = MyHelper.class).

package my.own.template.context.contributor;

import com.liferay.portal.kernel.template.TemplateContextContributor;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import my.own.template.object.MyHelper;

@Component(
    immediate = true,
    property = "type=" + TemplateContextContributor.TYPE_GLOBAL,
    service = TemplateContextContributor.class
)
public class MyDisplayTemplateContextContributor
    implements TemplateContextContributor {

  @Override
  public void prepare(Map<String, Object> contextObjects, HttpServletRequest httpServletRequest) {
    contextObjects.put("myHelper", _myHelper);
  }

  @Reference(unbind="-")
  MyHelper _myHelper;

}

After deploying that module, all Freemarker templates in your Liferay instance shall have the variable myHelper, with all public methods you add in the class MyHelper -- one of them could return the revision number you need.

Upvotes: 0

pratik patel
pratik patel

Reputation: 326

try this :

set ($releaseLocalService = $serviceLocator.findService("com.liferay.portal.service.ReleaseLocalService"))
#set ($release = $releaseLocalService.getRelease(1))
#set ($vers = $release.getBuildNumber())

<span class="simpleVersion">$vers</span>

Upvotes: 1

Related Questions