Arena Red » 10 Nov 2004 » Velocity Template Paths
« Post-Election Playback | How Not to Change Your Oil »
Velocity Template Paths

I'm in the process of throwing away a Java library I wrote a couple of years ago that imlements a templating engine mainly for building dynamic HTML pages like the ones on this site. I'm simplifying my life now by unburdening myself of all that code, and switching to the very nice Velocity template engine from the Apache project. It was pretty easy to get started with Velocity on my test system, but I ran into trouble on the hosted system where the real web site is deployed.

The issue is how to intialize Velocity so that it knows where to find templates. By default, it looks for resources like templates by using the file path you supply, relative to the working directory. On my local dev/test system, that's just the directory from where I launch Tomcat. But on the hosted system, which provides a very limited administrative interface, I simply have not been able to figure out where the working directory is. It may not even be something I can use with relative paths to my files. I only have knowledge of the full path to my hosted file system area.

I finally figured out how to initialize Velocity to direct it to my specified directory on the hosted system.

Example Environment

Suppose your templates are located in a directory whose full path is:

/home/youraccount/public_html/velocity_templates/

And suppose your Tomcat WEB-INF directory full path is:

/home/youraccount/public_html/WEB-INF/

Solution

Here is how you might set up the system so that Velocity will find templates using your template directory as the base path for templates:

Create a properties file in the WEB-INF directory. We'll call it velocity.properties. Here are the contents:

resource.loader = file

file.resource.loader.description =
  Velocity File Resource Loader
file.resource.loader.class =
  org.apache.velocity.runtime.resource.loader.FileResourceLoader
file.resource.loader.path =
  /home/youraccount/public_html/velocity_templates
file.resource.loader.cache = false
file.resource.loader.modificationCheckInterval = 2

The key item there is file.resource.loader.path. It specifies the full path your template root folder.

To initialize Velocity, you can then use the overloaded init() function that takes a path to the properties file:

import org.apache.velocity.app.Velocity;
...
Velocity.init("/home/youraccount/public_html/WEB-INF/velocity.properties");

Finally, to retrieve a template from the directory indicated by the file.resource.loader.path property, you can simply specify the file name:

template = Velocity.getTemplate("my_template_file.txt");

This allows you to use Velocity templates without having access to the Tomcat working directory path.

Top 10 of 386 Referrers:
[19] http://www.cadenhead.org/workbench/category/Java
[17] Google: "velocity template root"
[16] http://www.cadenhead.org/workbench/
[13]
[12] Google: "velocity template path"
[7] Google: "velocity template directory"
[6] http://www.cadenhead.org/workbench/category/Java/2509
[5] Google: "velocity file.resource.loader.path"
[5] Google: "velocity template path"
[4] Google: "file.resource.loader.path"