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
25
votes
4 answers

Velocity templates seem to fail with UTF-8

I have been trying to use a velocity Template with the following content: Sübjäct $item Everything works fine except the translation of the two Unicode characters. The result string printed on the command line looks like: Sübjäct foo I searched…
steve
  • 353
  • 1
  • 3
  • 5
25
votes
4 answers

velocity template and javascript

I try add javascript to my velocity template. :: $currency.CurrencyName Detail Info ::
Name $currency.CurrencyName
Jual
Adi Sembiring
  • 5,798
  • 12
  • 58
  • 70
25
votes
8 answers

What is the best way to access an array inside Velocity?

I have a Java array such as: String[] arr = new String[] {"123","doc","projectReport.doc"}; In my opinion the natural way to access would be: #set($att_id = $arr[0]) #set($att_type = $arr[1]) #set($att_name = $arr[2]) But that it is not…
Sergio del Amo
  • 76,835
  • 68
  • 152
  • 179
24
votes
1 answer

Is there a way to break from a foreach loop in velocity?

I'm looking for a certain condition by using foreach to iterate through a collection (of permissions). So if I find all that I need and don't need to loop anymore, is there a way to break out of the loop? I am new to velocity and trying to grok…
barneytron
  • 7,943
  • 3
  • 23
  • 25
23
votes
9 answers

Velocity and $foreach.count

I am using velocity 1.7 and within a foreach loop I want to print the count. In the template I have the following string in a #foreach/#end section: Count: $foreach.count and was expecting to see in the rendered result something like Count:…
yannisf
  • 6,016
  • 9
  • 39
  • 61
23
votes
5 answers

Using velocity split() to split a string into an array doesnt seem to work

I HATE velocity and rarely ever use it but sometimes I am called upon at my job to do so. I can never really figure out just how to use it. I have this #foreach( $product in $browseSiteProducts ) alert("$product.productId"); #foreach(…
kamcknig
  • 883
  • 3
  • 9
  • 27
23
votes
1 answer

NHibernate L2 Cache configuration in Fluent NHibernate

Is ti possible to configure the L2 cache provider in code via FHN? Adding a line to the following config is what I'm after: return Fluently.Configure() .Database(MsSqlConfiguration.MsSql2005.ConnectionString(c =>…
AwkwardCoder
  • 24,893
  • 27
  • 82
  • 152
23
votes
4 answers

VelocityEngineUtils has been removed in Spring 3.2 so what else to use?

Today I have upgraded my entire Spring web application from using Spring 3.1.1 to Spring 3.2. Most part of my existing app does not break except that in Spring 3.2, the org.springframework.ui.velocity.VelocityEngineUtils class seems to be…
woraphol.j
  • 1,301
  • 5
  • 14
  • 22
22
votes
6 answers

Calling Macro function in Velocity template

I am trying to figure out how to return a value from a velocity macro call and assign it to a varaible my macro function looks something like this. its once in common shared macros files #macro(getBookListLink, $readingTrackerResult) …
user747858
22
votes
5 answers

Freemarker vs. Velocity

I'm trying to decide between Freemarker and Velocity to use as a template engine, any advantages/disadvantages you see regarding each? Any alternatives that I should look at instead?
Tony the Pony
  • 40,327
  • 71
  • 187
  • 281
22
votes
1 answer

Checking if a string contains a particular substring in Velocity

In Velocity I have a variable called $url which contains the following string: [ContentId(2.7507), ContentId(2.7508), ContentId(1.44551)] I want to check if that string contains the substring 1.44551. This is the code I've written so far, but for…
Victoria
  • 901
  • 3
  • 15
  • 30
21
votes
8 answers

How can I trim whitespace by Velocity

I have a method called render_something which can creates a lot of whitespace, for example: #render_something('xxx') The result can be: something that generate from redner_something …
Surasin Tancharoen
  • 5,520
  • 4
  • 32
  • 40
21
votes
3 answers

Replacing variables in a word document template with java

I want to load a template word document to add content to and save as new document. I'm working on .doc file. After a long research I only found solutions for docx :…
Amira
  • 3,184
  • 13
  • 60
  • 95
21
votes
1 answer

How to #include a file from a Velocity template using ClasspathResourceLoader

I'm dealing with some Java code where Velocity 1.7 is set to retrieve templates through the ClasspathResourceLoader. Below is a stripped down sample of the code. It's from a Tapestry web application running on a Jetty server. The Java class, the…
SantiBailors
  • 1,596
  • 3
  • 21
  • 44
20
votes
2 answers

AppSync: Nested Type Resolver

I try to include nested types defined in the following graphql schema: type User { id: String! posts: [Post] } type Post { id: String! } type Query { getUser(id: String!): User getPost(id: String!): Post } As you can see a User has…
DrDirk
  • 1,937
  • 3
  • 25
  • 36