Pages

Saturday 14 April 2012

How to set and use custom Velocity variables in Liferay


How to set and use custom Velocity variables in Liferay
First things first: what is Velocity?
Velocity is a popular template engine for Java web applications. It’s one of the many useful projects of Apache. Liferay uses an implementation of the Velocity engine to render Velocity templates. These are files that typically have a vm extension.
All Liferay themes make use of this Velocity template engine. A template engine is software that is designed to process web templates (in this case files with a vm extension) to produce output web documents. These web documents are mostly HTML or XML files, but it can also be complete different things like CSS or JS files.
Using a template engine also separates business logic from presentation, which gives the maintainability of your source code a boost. This is also known as the MVC software architecture. In the controller section of your web application you can set variables which you can use to display or do other things in the view section of your web application.
By default Liferay already sets a lot of these Velocity variables. If you want to see them all, you can check com.liferay.portal.events.ServicePreAction. In some occasions in can be useful to set some custom Velocity variables yourself. In the following post you’ll find out how.
How do you set custom Velocity variables?
You need to do two things to set custom Velocity variables:
  1. Create a new portal event in Liferay (create a new Java class).
  2. Edit Liferay properties (the portal-ext.properties file in the Extension Environment).
Create portal event
You need an extra event that’s being executed by any request to the web server.
In this event, you can define your own custom Velocity variables dynamically.
package sri.sample.portal.events;

import com.liferay.portal.kernel.events.Action;
import com.liferay.portal.kernel.events.ActionException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.liferay.portal.util.WebKeys;
import java.util.Map;
import java.util.HashMap;
import java.util.Calendar;
import java.lang.String;
import java.lang.Object;

public class ServicePreAction extends Action {
        public void run(HttpServletRequest request, HttpServletResponse response)
               throws ActionException {
               Map vars = new HashMap();
               Calendar c = Calendar.getInstance();
               vars.put("year", c.get(Calendar.YEAR));
               vars.put("month", c.get(Calendar.MONTH) + 1);
               vars.put("day", c.get(Calendar.DAY_OF_MONTH));
               vars.put("tagline", "This is the tagline of the site,
               coming from somewhere in the database!");
               request.setAttribute(WebKeys.VM_VARIABLES, vars);
        }
}
Edit Liferay properties
Open the portal-ext.properties file in the Extension Environment and search for this property:
servlet.service.events.pre=com.liferay.portal.events.ServicePreAction
Append the new portal event to this property like the following line of code:
servlet.service.events.pre=com.liferay.portal.events.ServicePreAction,\
sri.sample.portal.events.ServicePreAction
That’s all you need to do!
You can now use these Velocity variables in any template file (vm extension) of a Liferay theme like this:
$tagline

This is the current date: $year-$month-$day

1 comment:

  1. how to make the custom variables visible in the web content template VM

    ReplyDelete