Getting the themeDisplay object in DXP themes and layout templates

Issue

  • 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?

Environment

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

Resolution

  • 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: https://docs.liferay.com/dxp/digital-enterprise/7.0-latest/propertiesdoc/portal.properties.html#Module%20Framework%20Web%20Application%20Bundles
        module.framework.web.generator.default.servlet.packages=\
            com.liferay.portal.kernel.service,\
    com.liferay.portal.model,\ com.liferay.portal.security.access.control,\ com.liferay.portal.security.auth,\ com.liferay.portal.security.permission,\ 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.stream,\ com.sun.el.util,\ org.apache.commons.chain.generic,\ org.apache.naming.java,\ \ # # WebSocketSupport # \ com.ibm.websphere.wsoc,\ 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\liferay-plugin-package.properties 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() />
    <ul>
    <li>${themeId}</li>
    <li>${groupId}</li>
    <li>${companyId}</li>
    </ul>
    </#if>

Additional Information

这篇文章有帮助吗?
1 人中有 1 人觉得有帮助