Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
Mahmud Hossain
Truncating text in Liferay velocity
December 11, 2012 12:44 PM

Mahmud Hossain

Rank: New Member

Posts: 18

Join Date: May 28, 2012

Recent Posts

I'm looking for ways to truncate text using velocity in liferay. I can use javascript to do this work, but i think it would more suitable for me to truncate text using velocity. I found this class called DisplayTool which let's you truncate text but i don't think it's included with liferay.
David H Nebinger
RE: Truncating text in Liferay velocity
December 13, 2012 11:07 AM

David H Nebinger

Community Moderator

Rank: Liferay Legend

Posts: 13021

Join Date: September 1, 2006

Recent Posts

When constructing a velocity context, most of the inserted object references are hard coded in com.liferay.portal.velocity.VelocityVariablesImpl.

However, the code is actually somewhat dynamic in that it will also inject values from the "VM_VARIABLES" attribute on the HttpServletRequest.

So you could define a servlet filter for the ROOT application that implements a wrapper around the HttpServletRequest. In your wrapper, you'd override the getAttribute() method, something like this:

 1public Object getAttribute(final String attributeName) {
 2  if ((attributeName == null) || (attributeName.trim().length() < 1) return wrappedRequest.getAttribute(attributeName);
 4  if (!(WebKeys.VM_VARIABLES.equals(attributeName))) return wrappedRequest.getAttribute(attributeName);
 6  Map<String,Object> vmVars = (Map<String,Object>) wrappedRequest.getAttribute(attributeName);
 8  if (vmVars == null) vmVars = new HashMap();
10  if (vmVars.get("displayTool") == null) {
11    vmVars.put("displayTool", new DisplayTool());
12  }
14  return vmVars;

This will allow you to dynamically inject your DisplayTool instance and will make it available to the velocity context and therefore your velocity templates.
Hitoshi Ozawa
RE: Truncating text in Liferay velocity
December 13, 2012 2:11 PM

Hitoshi Ozawa

Rank: Liferay Legend

Posts: 7949

Join Date: March 23, 2010

Recent Posts

Following page may help you.