Foren

Truncating text in Liferay velocity

Mahmud Hossain, geändert vor 11 Jahren.

Truncating text in Liferay velocity

New Member Beiträge: 18 Beitrittsdatum: 28.05.12 Neueste Beiträge
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.
thumbnail
David H Nebinger, geändert vor 11 Jahren.

RE: Truncating text in Liferay velocity

Liferay Legend Beiträge: 14919 Beitrittsdatum: 02.09.06 Neueste Beiträge
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:

public Object getAttribute(final String attributeName) {
  if ((attributeName == null) || (attributeName.trim().length() < 1) return wrappedRequest.getAttribute(attributeName);

  if (!(WebKeys.VM_VARIABLES.equals(attributeName))) return wrappedRequest.getAttribute(attributeName);

  Map<string,object> vmVars = (Map<string,object>) wrappedRequest.getAttribute(attributeName);

  if (vmVars == null) vmVars = new HashMap();

  if (vmVars.get("displayTool") == null) {
    vmVars.put("displayTool", new DisplayTool());
  }

  return vmVars;
}</string,object></string,object>


This will allow you to dynamically inject your DisplayTool instance and will make it available to the velocity context and therefore your velocity templates.
thumbnail
Hitoshi Ozawa, geändert vor 11 Jahren.

RE: Truncating text in Liferay velocity

Liferay Legend Beiträge: 7942 Beitrittsdatum: 24.03.10 Neueste Beiträge
Following page may help you.

http://www.sergiy.ca/how-to-create-custom-tools-for-apache-velocity/