Getting the themeDisplay object in DXP themes and layout templates


  • In Liferay Portal 6.2, the themeDisplay object was inherited in themes and layout templates. How is it possible to get the same object in DXP?


  • Liferay DXP 7.0
  • Liferay DXP 7.1
  • Liferay DXP 7.2
  • Liferay DXP 7.3


  • For 7.0, this can be an example:
    <#assign serviceContext = staticUtil["com.liferay.portal.kernel.service.ServiceContextThreadLocal"].getServiceContext()>
    <#assign themeDisplay = serviceContext.getThemeDisplay() />
    In order for the above snippet to work, the import of com.liferay.portal.kernel.service package shall be declared. This can be done in two ways:

    a) At Portal level, through the module.framework.web.generator.default.servlet.packages portal property:
    com.liferay.portal.model,\,\,\,\ com.liferay.portal.service,\ com.liferay.portal.servlet,\ com.liferay.portal.servlet.filters.aggregate,\ com.liferay.portal.servlet.filters.dynamiccss,\ com.liferay.portal.osgi.web.servlet.jsp.compiler,\ com.liferay.portal.spring.context,\ com.liferay.portal.theme,\ com.liferay.portal.util,\ com.liferay.portlet,\ com.sun.el,\ com.sun.el.lang,\ com.sun.el.parser,\,\ com.sun.el.util,\ org.apache.commons.chain.generic,\,\ \ # # WebSocketSupport # \,\ io.undertow.websockets.jsr,\ javax.websocket,\ javax.websocket.server,\ org.apache.tomcat.websocket.server,\ org.eclipse.jetty.websocket.server,\ org.glassfish.tyrus.servlet,\ weblogic.websocket.tyrus
    2) At WAR level, by adding:
    Import-Package: com.liferay.portal.kernel.service;resolution:=optional
    in the war's WEB-INF\ file.

  • In 7.1+, the themeDisplay object is available out of the box in Freemarker templates, you may simply use it. The only step to take care of is nullchecking the themeDisplay variable so that it does not trigger errors upon deployment:
    <#if themeDisplay??>
    <#assign themeId = themeDisplay.getThemeId() />
    <#assign groupId = themeDisplay.getScopeGroupId() />
    <#assign companyId = themeDisplay.getCompanyId() />

Additional Information

1 人中有 1 人觉得有帮助