473,439 Members | 1,716 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,439 software developers and data experts.

Quick Velocity Template Example

Oralloy
988 Expert 512MB
Folks,

A co-worker and I are writing plug-ins for the Atlassian tools JIRA and Confluence.

Our plug-ins for JIRA are using Velocity (see http://org.apache.velocity) templates effectively. The "Report" object I'm using implicitly loads the template and generally hides Velocity from me, so all we have to do is populate a Map<String, Object> name/value pairs and write the supporting Velocity template.

Unfortunately for Confluence, we're stuck with a model which does not implicitly invoke a template. I've been trying to find an example in the on-line developers code base that does what I'd like, but so far I'm batting zero.

The basic problem is that I really don't understand in-depth how things wire together, so we're stuck programming by copy/example at the moment. This is very frustrating, but we'll get past it.

In the meanwhile, is there a method that I can use to process a Velocity template in-line in the code and get the resulting string? Once I have the string, it's easy enough to plug it into the Confluence output stream and display whatever I want.

I'm pretty certain that the answer is three lines of code, please help by filling in the blanks ....

Expand|Select|Wrap|Line Numbers
  1. Map<String, Object> values = new Map<String, Object>;
  2. values.add("name1", object1);
  3. values.add("name2", object2);
  4.  
  5. String templateBody = lookupResource("template-id");
  6.  
  7. VelocityTemplate template = new VelocityTemplate(templateBody);
  8.  
  9. String result = template.apply(values);
  10.  
Thanks for your help,
Oralloy
Jul 23 '10 #1
1 3845
Oralloy
988 Expert 512MB
And some days we eventually come to a solution.

For anyone who may need it:

Expand|Select|Wrap|Line Numbers
  1. import org.apache.velocity.app.Velocity;
  2. import org.apache.velocity.VelocityContext;
  3. import org.apache.velocity.Template;
  4.  
  5. import java.io.*;
  6. import java.util.ArrayList;
  7.  
  8. public class SimpleInstance {
  9.   final String propertyFile = "SimpleInstance.properties";
  10.   final String templateFile = "SimpleInstance.vm";
  11.  
  12.   public static void main(String[] argv) {
  13.     try {
  14.       Velocity.init();
  15.       Velocity.init(propertyFile);
  16.  
  17.       VelocityContext context = new VelocityContext();
  18.       String value = "value string";
  19.       context.put("value", value);
  20.  
  21.       Template template =  Velocity.getTemplate(templateFile);
  22.  
  23.       BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
  24.       template.merge(context, out);
  25.     }
  26.     catch(Exception exception) {
  27.       exception.printStackTrace();
  28.     }
  29.   }
  30. }
Cheers!
Jul 27 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: SpeedBump | last post by:
This is mostly just another "gee it would be nice if it had X" post. Recently I have come across two separate problems which both exhibit the need for a way to self reference when instantiating a...
7
by: Leon | last post by:
I'm having a bit of a template-problem, and I was wondering if you guru's know what I'm doing wrong here... I want to create a Vertex class that supports a toPoint() method. Now, in my case...
5
by: christopher.secord | last post by:
Here's a quick and dirty example of a legitimate bug in SQL Server. I've seen other examples, but they were all very complex, some even involving cursors and such. This one will produce the error...
9
by: blueblueblue2005 | last post by:
Here is a very simple template example, whenever I seperate the header and implementation file, I got "undefined reference to member function error. and if I put the header and definition in one...
2
by: Bilal | last post by:
Hello, I'm stuck on this problem for quite some time and hope somebody would be able to guide me. Basically, I need to populate a large number of "template" XML files which have all...
1
by: lele1979 | last post by:
Hi XSL Community, I would do a file in PDF with FO and XSL/XML tecnology. In a precisely moment of a creation of this PDF file I must call a precisely template from a precisely XSL file, that...
3
by: IR | last post by:
Hi, I've been trying to do the following (which doesn't compile) : template<class T, class F = Example<T struct Example { F foo(); };
2
by: mfabricius | last post by:
Hi, I am sure it's silly. I get the error: /tmp/ccyVx871.o: In function `main': UseStack.cpp:(.text+0x8b): undefined reference to `Stack<int>::Stack()' UseStack.cpp:(.text+0x9e): undefined...
0
by: cakeathon | last post by:
I am trying to find a simplified template example of the STL list container on the web without much success. Please can some one point me in the right direction ?
4
by: nooneinparticular314159 | last post by:
I'm trying to understand template metaprogramming syntax. It's been years since I've touched C++, so this may actually be a C++ syntax issue (although it seems that the language has changed...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.