Questions tagged [velocity]

Apache Velocity is a Java-based template engine. It can be used to dynamically generate web pages, email messages, source code, or any other text file.

Apache Velocity is a Java-based template engine. It can be used to dynamically generate web pages, email messages, source code, or any other text file.

Apache Velocity is a project of the Apache Software Foundations (ASF).

More information is available at the project home page: http://velocity.apache.org.

From the developer perspective, the fundamental pattern for rendering a text file is:

  • Create and initialize a VelocityEngine.
  • Add your data objects to a Context (essentially, a map of objects available for use in the template)
  • Choose a template
  • Merge the template with the context.

This can be done in a few lines of Java code, or you can use one of several frameworks to integrate this with your application.

One of the most popular web application frameworks which integrates Velocity is the Spring Framework. For simple servlet based applications, most developers use one of the Velocity Tools servlets (also available from the Apache Velocity project).

Useful Online Resources

  1. Velocity User Guide
  2. Velocity Developer Guide
2370 questions
13
votes
1 answer

Output part of a string in velocity

apologies if I waffle or talk a bit of jibberish but I'm new to velocity, and these forums! I need to check the contents of a string for a certain character and output the second part of the text if it appears. For example: set ($string = "This is a…
OnionSandwich
  • 237
  • 1
  • 3
  • 9
13
votes
3 answers

conditional operator in Velocity

Is there a way to do ternary operators in Velocity? This is what I'd like to do: #set ($name = ($args.get(0) == "") ? "default" : $args.get(0)) Instead of chunky if-else #if ($args.get(0) == "") #set ($name = "default") #else #set ($name =…
peirix
  • 36,512
  • 23
  • 96
  • 126
12
votes
4 answers

How to remove duplicate elements from a list using apache velocity

I have a list with duplicate elements,I need to use velocity For Example, posts contains duplicate elements #foreach ($p in $posts) $p.name //will be unique #end I want to remove the duplicate using velocity, Any help would be appreciated
imby
  • 121
  • 1
  • 3
12
votes
5 answers

Velocity - How to avoid ParseErrorException when using jQuery?

I'm trying to add a jQuery post to some JavaScript on a web page. The entire page is built up of several Velocity templates. Everything has been fine until I've tried to add the jQuery post, now I…
Michael
  • 7,348
  • 10
  • 49
  • 86
12
votes
4 answers

Spring MVC: Resolving the view based on User-Agent

Spring Version: 2.5.6 I want to resolve the view to a specific velocity file based on the value of the User-Agent header. My current line of thinking is an implementation similar to the UrlBasedViewResolver such that the user-agent value is Map'd…
bearded_devil
  • 262
  • 2
  • 11
12
votes
2 answers

Check if a map is empty in Apache Velocity

In my java/spring app, I have a velocity template in which I create a map which will hold values also inserted in the template: #set ($myMap = {}) What I want to do is have an if/else checking if the map is empty. This doesn't seem to be working…
John Farrelly
  • 7,289
  • 9
  • 42
  • 52
12
votes
1 answer

Quiver plot arrow aspect ratio

I am having some issues with Matplotlib's quiver plot. Given a velocity vector field, I want to plot the velocity vectors on top of the stream lines. The vectors are not tangent to the stream function as expected. To calculate the stream function, I…
regeirk
  • 948
  • 1
  • 7
  • 16
12
votes
3 answers

Is there a way to debug Velocity templates in the traditional code debugging sense?

We make heavy use of Velocity in our web application. While it is easy to debug the Java side of things and ensure the Velocity Context is populated correctly, it would be extremely valuable to be able to step through the parsing of the VTL on the…
Peter
  • 29,498
  • 21
  • 89
  • 122
11
votes
1 answer

Can you define a velocity macro to "wrap" other content?

I'm trying to abstract a common scenario in generated markup where I need a couple of tags to "wrap" an arbitrary content. So instead of writing this

Some header