A PHP Error was encountered

Severity: Notice

Message: Undefined variable: flickr_photo

Filename: models/flickr_model.php

Line Number: 25

How to set and use custom Velocity variables in Liferay - Blog - Webdesign E-KON
No Dutch
Startpagina » Blog » How to set and use custom Velocity variables in Liferay Share/Bookmark

How to set and use custom Velocity variables in Liferay

Op 05 november 2010 door Kristof Verbraeken in categorie 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 occations 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 be.ekon.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,\
be.ekon.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

 



Reacties

o4g2j9ZbK5l ()

It's curious that serehps are slower than cubes. It sounds like a nuance of the game engine.I'm curious to see what you find about the way other entities fall. I'm hoping there's some type of relationship between mass, shape and terminal velocity.I've got a really neat experiment coming up for tomorrow's video. After that, I'd like to figure out a way to quantitatively answer how your acceleration changes as you approach terminal velocity. Do you think it'd be easiest to just use a mix of a really long fall with host_timescale and cl_showpos? Know of any quirks with cl_showpos and the way it reports velocity?

kkiJZmed ()

That's a sensible answer to a challenging question

Reactie plaatsen

( Je email is veilig bij ons! )

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/index.php

Line Number: 181