• Context Contributor Concept in Liferay 7/DXP

    Posted on May 11, 2017 by Hamidul Islam in liferay 7.


    Context Contributor simply provides contextual information to template files like Velocity or Free Marker Template. Templates are widely used in Application Display Template (ADT), Web Content Display Portlet and Liferay Theme. Using Context Contributor we can write piece of code to inject contextual information which can be reused in various template files. In this article we will explain the use of Context Contributor.

    Whats exactly Context Contributor?

    To make it little simple lets consider that you are developing a liferay theme. So its obvious that you will use either FreeMarker Template(FTL) or Velocity Template(VM). Now you want to display the greeting message (for example Hello John, welcome to our portal) some where in the Theme Level for logged in user with his full name. Similarly for not logged in user you want to display You are not logged in. In liferay 6.2 you might be thinking to  to write your logic in  init.vm /init.ftl file which is very simple. But think that you are developing another Theme where you need the same functionality. You will simply copy & paste the existing logic to the new theme. Using Context Contributor you can overcome this kind of situation. You can write your logic only once and reuse all the time whenever required.

    Writing the Sample Context Contributor:

    Maven can be used to write the Context Contributor and it has archetype for it. The name of the archetype is com.liferay.project.templates.template.context.contributor. Lets create the sample project using this archetype. Below is the whole command (should be in single Line) which is used to create the project.

    The above command will create the project and we can import the project as maven project in the eclipse. Below is the class file


    1.  We must implement the prepare method of TemplateContextContributor interface

    2. We have request object in the prepare method. So retrieving contextual information is easy

    3. contextObjects.put("welcomeMessage",welcomeMessage). That means welcomeMessage is injected as key in contextObjects which is a map. Later on we can retrieve the key value in any template file.

    4. property = {"type=" + TemplateContextContributor.TYPE_GLOBAL} means the context information would be available for the entire portal. It can also be property = {"type=" + TemplateContextContributor.TYPE_THEME} which means that the contextual information is only for the theme level.

    5. Give attention to service = TemplateContextContributor.class. For the context contributor project  this is must.


    The final artifact of the above project would be an OSGI Module which need to be deployed before testing. For better understanding of the artifact refer the below pom.xml

    How to retrieve value of welcomeMessage?

    Its quite simple.In velocity template(VM) just add $welcomeMessage or in FreeMarker Template (FTL) it should ${welcomeMessage}. 




    Post Tagged with , ,
Comments are closed.
%d bloggers like this:

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.