Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Velocity, different template paths

Tags:

java

velocity

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 Properties();
        p.setProperty("resource.loader", "file");
        p.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
        p.setProperty("file.resource.loader.path", "");

    Velocity.init(p);
}

The templates can be located in different locations ( the user can select one with a file dialog ). So I have this code upon fetching the template out of velocity

private Template fetch (String templatePath) {
    out_println("Initializing Velocity core...");
    int end = templatePath.lastIndexOf(File.separator); 

    Properties p = new Properties();
        p.setProperty("file.resource.loader.path", templatePath.substring(0, end));
    Velocity.init(p);

    return Velocity.getTemplate(templatePath.substring(end+1));
}

This is not working. It seems that once Velocity is initialized it can't be reset with different properties. Any suggestions on how to solve this problem?

Possible Program flow:

  1. User selects group that needs to be filled into the template
  2. User selects a template to use (can be located anywhere on the hdd)
  3. User presses generate
like image 954
Arninja Avatar asked Oct 01 '12 07:10

Arninja


People also ask

What are Velocity templates?

Velocity is a server-side template language used by Confluence to render page content. Velocity allows Java objects to be called alongside standard HTML. If you are are writing a user macro or developing a plugin you may need to modify Velocity content.

Is Velocity template deprecated?

Velocity templates were deprecated in Liferay Portal 7.0 and are now removed in favor of FreeMarker templates in Liferay DXP 7.2.

How do I test a Velocity template?

You can test Velocity templates in your web application by integerating Cactus with VelocityViewServlet. This assumes that you are running your web application at http://localhost:8080/CactusDemo/ .


2 Answers

Adding to the points above:

Even if one is using non-singleton model i.e using VelocityEngine object. Multiple paths can be configured by giving comma separated values to the property.

[file.resource.loader.class=path1,path2]

In such a case velocity engine will look for template in path1 first and then in path2

like image 100
ashish Avatar answered Oct 13 '22 16:10

ashish


Velocity can be used in two ways: the singleton model or the separate instance model. You are currently using the singleton model in which only one instance of the Velocity engine in the JVM is allowed.

Instead, you should use the separate instance model which allows you to create multiple instances of Velocity in the same JVM in order to support different template directories.

VelocityEngine ve = new VelocityEngine();
ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, "path/to/templates");
ve.init();
Template t = ve.getTemplate("foo.vm");
like image 23
dogbane Avatar answered Oct 13 '22 15:10

dogbane