Liferay Service & Util access in Liferay theme Velocity Template

Liferay Service & Util access in Liferay theme Velocity Template  tutorial will give light on accessing  Liferay Services and Utilities at theme level. Liferay provides init_custom.vm file to hold custom variables.

  • $serviceLocator.findService is used to load services (service folder classes service.jar) into theme velocity template
  • $utilLocator.findUtil() is used to load util classes (util package classes in service.jar) into liferay theme velocity template

you can see the in this tutorial to see full

Liferay Default Theme Velocity Variables

Liferay Default Theme Velocity Variables will show light on the available velocity variables in Liferay theme. As you already know, Liferay already set parent variables in  READ MORE

 

How to access Liferay Core Services in Liferay theme velocity:

GroupLocalService in Liferay Theme velocity:
  • #set($groupService = $serviceLocator.findService(“com.liferay.portal.service.GroupLocalService”))
  • #set($userGroupList = $groupService.getUserGroups($theme_display.getUserId()))

UserLocalService in Liferay theme velocity:

  • #set($userService = $serviceLocator.findService(“com.liferay.portal.service.UserLocalService”))
  • #set($userid= $userService.getUser($theme_display.getUserId()))
LayoutLocalService object in Liferay theme velocity:

#set($layoutService = $serviceLocator.findService(“com.liferay.portal.service.LayoutLocalService”))

How to get user roles in Liferay theme velocity template?

#set($current_role=””)

#foreach ($user_role in $user.getRoles())
#if($user_role.getName().equalsIgnoreCase(“role1″))
#set($current_role=”role1”)
#elseif($user_role.getName().equalsIgnoreCase(“admin”))
#set($current_role=”Admin”)
#end
#end

How to Access Liferay Core Utilities in Liferay Theme velocity:

$utilLocator.findUtil is used to load

#set($webTokenService = $utilLocator.findUtil(“com.liferay.portal.webserver.WebServerServletTokenUtil”))

How to Access Liferay Custom Portlet Services In Liferay Theme Velocity template

  • Copy the custom-portlet-service.jar into tomcat/lib/ext folder
  • You can access the custom service like native core services with servicelocator
  • You need to pass the Service name that can be found in “WEB-INF/service/{package-path}/service/XXXXXXService.java”
  • #set($studentService = serviceLocator.findService(“com.student.service.StudentLocalService”))