Liferay Template Context Contributors

Liferay Template Context Contributors


Overview

Liferay provides a lot of predefined variables and utils to simplify work for template developers. If you work on theme development, widget template or fragments implementation - you can use these variables in your freemarker code. But for certain scenarios you may need your custom objects to be exposed for templates. In this case Template Context Contributor is the rescue.


Template Context Contributors Overview

Liferay has a dedicated interface to expose custom objects for freemarker templates - TemplateContextContributor. It contains a simple method, which needs to be implemented in order to put custom objects to the contextObjects map. HttpServletRequest object is passed as a second argument, and it can be used to fetch required information from request parameters or attributes:


public void prepare(Map<String, Object> contextObjects, HttpServletRequest httpServletRequest);


Also, two constants are defined in this interface to specify the type of contributor: theme (objects are exported for theme templates only), or global (objects are exported globally - for theme templates, widget templates, fragments, etc.).

public static final String TYPE_GLOBAL = "GLOBAL";

public static final String TYPE_THEME = "THEME";


An example of Liferay Template Context Contributor can be found in BrowserTemplateContextContributor.java. This class defines a "bodyCssClass" variable, which is then used in theme's templates.


Custom Template Context Contributor

To implement a custom Template Context Contributor we need to create a module with the following structure:


build.gradle:


dependencies {

  compileOnly group: "com.liferay.portal", name: "release.portal.api"

}


bnd.bnd:


Bundle-Name: LifeDev Template Context Contributor

Bundle-SymbolicName: com.lifedev.template.context.contributor

Bundle-Version: 1.0.0


LifeDevTemplateContextContributor.java 


@Component(

       property = "type=" + TemplateContextContributor.TYPE_GLOBAL,

       service = TemplateContextContributor.class

)

public class LifeDevTemplateContextContributor implements TemplateContextContributor {


   @Override

   public void prepare(Map<String, Object> contextObjects, HttpServletRequest httpServletRequest) {

       contextObjects.put("platform", "Liferay");

   }


}


This is a class, where we need to define the logic for contributing our custom objects.

It’s a regular OSGi component - that’s why it has a @Component annotation. 

Type property is set to GLOBAL to contribute objects globally.

Service is set to TemplateContextContributor.class, because it’s a component for Template Context Contributor.

In the prepare method we put a custom object to the contextObjects map.

After a module is deployed - we can access our objects in freemarker templates - for example, in fragment’s HTML code:



Enjoy 😏

Comments

Popular posts from this blog

Liferay Search Container Example

Liferay DXP - max upload file size

Liferay Keycloak integration