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
16
votes
3 answers

getClass().getClassLoader().getResourceAsStream() is caching the resource

I have a resource (velocity template) which I'd like to be able to swap during development. However, getClass().getClassLoader().getResourceAsStream() seems to cache the template. Is there a way to disable this besides using a file loader…
Mike
  • 1,176
  • 3
  • 14
  • 26
16
votes
1 answer

Apache Velocity $ vs $!{}

For apache velocity, is there a difference between accessing variables with $ vs. $!{}. If so, what is it?
Luke
  • 1,209
  • 2
  • 10
  • 7
16
votes
4 answers

How to disable velocity logs

I've been trying to disable Velocity logs, and the only way I've found so far with positive results is to set: runtime.log.logsystem.class=org.apache.velocity.runtime.log.NullLogSystem but inside the velocity.properties that resides inside the…
luiso1979
  • 868
  • 1
  • 6
  • 18
15
votes
4 answers

How to get a zero-based count in a Velocity foreach loop

I am trying to get a zero-based counter in a Velocity #foreach directive. if i use: #foreach ($item in $list) item.getName() : $velocityCount #end i will get: Fred : 1 Wilma : 2 Barney : 3 But i need: Fred : 0 Wilma : 1 Barney : 2 The solution…
summerbulb
  • 5,709
  • 8
  • 37
  • 83
15
votes
3 answers

Unsetting a variable in Velocity

Is it possible to set a Velocity reference to 'null' or 'undefined'? The Velocity template language reference says #set - Establishes the value of a reference Format: # [ { ] set [ } ] ( $ref = [ ", ' ]arg[ ", ' ] ) Usage: $ref - The LHS of the…
Simon Nickerson
  • 42,159
  • 20
  • 102
  • 127
15
votes
2 answers

Could not find org.springframework.boot:spring-boot-starter-velocity

I'm new to spring and trying to use velocity with spring boot. Here is my build.gradle repositories { mavenCentral() } plugins { id 'org.springframework.boot' version '2.0.4.RELEASE' } apply plugin: 'java' apply plugin: 'eclipse-wtp' apply…
sungyong
  • 2,267
  • 8
  • 38
  • 66
15
votes
2 answers

Why Velocity support is deprecated in Spring 4.3?

And how are we going to use Velocity with Spring after Spring 5.0?
Jun Guo
  • 484
  • 1
  • 3
  • 14
15
votes
2 answers

Accessing constant values from an Apache Velocity template?

Is it possible to access a constant value (i.e. a public static final variable defined in a Java class) from a Velocity template? I would like to be able to write something like this: #if ($a lt Long.MAX_VALUE) but this is apparently not the right…
Simon Nickerson
  • 42,159
  • 20
  • 102
  • 127
14
votes
5 answers

How to configure Velocity Escape Tool with Spring Properties?

I create e-mails from templates via Velocity in a Spring Web Application. Now I need to HTML escape SOME of the values. I found the Velocity Escape Tool. But I did not get the configuration working. What I have tryed so fare is (spring…
Ralph
  • 118,862
  • 56
  • 287
  • 383
14
votes
3 answers

Python Library - json to json transformations

Does anyone know of a python library to convert JSON to JSON in an XSLT/Velocity template style? JSON + transformation template = JSON (New) Thanks!
Kurt Maile
  • 1,171
  • 3
  • 13
  • 29
14
votes
1 answer

How to urlencode the asterisk

I'm trying to find what the urlencoded value of an asterisk would be. This is not for a URL, though, so please bear with me while I explain. The background: I'm working with a really old copy of Velocity (in Clickability), and I'm trying to build a…
Lisa
  • 2,102
  • 7
  • 26
  • 44
14
votes
1 answer

How can I provide custom logic in a Maven archetype?

I'm interested in creating a Maven archetype, and I think I have most of the basics down. However, one thing I'm stuck on is that sometimes I want to use custom logic to fill in a template. For example, if somebody generates my archetype and…
jith74
  • 141
  • 1
  • 4
13
votes
1 answer

How to capitalize one instance of a variable in a live template?

I am trying to make a template for React useState: const [item, setItem] = useState('value') const [$state$, set$state$] = useState($init$);$end$ Since item is the same, only with a capital I, is it possible to auto-capitalize the first letter? I…
ilyo
  • 35,851
  • 46
  • 106
  • 159
13
votes
3 answers

Velocity Template engine - key-value-map

I have some problems wo use a key-value-map into Velocity. Someone has an example of this functionality? $myMap ={} $myMap.put("mykey1", "myvalue") $myMap.delete("mykey1") $myMap.getValue("mykey1")
Wagner
  • 159
  • 1
  • 2
  • 5
13
votes
4 answers

Velocity, different template paths

Does anyone know if it is possible to get templates from different paths with velocity? After initialization Velocity refuses to change the "file.resource.loader.path". This is my code: public Generator(){ Properties p = new…
Arninja
  • 735
  • 1
  • 12
  • 24