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.