Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does Velocity search for the template?

Tags:

java

velocity

I need to use Velocity from Java-code in a web-application (I use it as mail-templates processor).

So, I have a standard code:

VelocityEngine ve = new VelocityEngine ();
try {
   ve.init ();
   Template t = ve.getTemplate (templatePath);
   ...   
} catch (Exception e) {
   throw new MailingException (e);
}

This code always throws the ResourceNotFoundException. Where should I place my templates in web-application (WEB-INF? classpath? etc?) and how should I specify path (i.e. what should I pass as templatePath)?

like image 727
Roman Avatar asked Jul 12 '10 13:07

Roman


People also ask

How do I test a Velocity template?

Approach 1: The obvious approach: Run and check. Run Velocity against the template to be checked. Look at the output and see if it is what you desired. This must be the most primitive testing approach, but most people nowadays are too lazy and want this done by the computer.

What is a Velocity template?

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.

How do I add an image to my Velocity template?

1 answer. Or in your Java code, try: URL url = new URL("image. png");


2 Answers

You need to initialize Velocity first by calling Velocity.init() (in the singleton model of usage), or VelocityEngine.init() (if you use a separate instance), and passing appropriate configuration parameters. These include the resource loader configuration.

Where to put your template files depends in which resource loader you choose - there are file, classpath, jar, url etc. resource loaders available.

If you use the file resource loader, the template path should be an absolute (directory/file) path. With the jar resource loader, though, it must not be an absolute path (if your templates are within a jar, that is). This is also true for links within your templates, i.e. if one of your templates includes another one by absolute path, the jar resource loader will fail to load it.

like image 82
Péter Török Avatar answered Sep 18 '22 21:09

Péter Török


SETTING UP VELOCITY TEMPLATES

I ended up using this question to solve my problem of setting up the template path. I am using velocity for templating html emails.

Here is a good couple of methods you can use that illustrate how the template path is set up. It sets the property 'file.resource.loader.path' to the absolute path. I created a directory for templates, and then right clicked on the template file to get the full path (In Eclipse). You use that full path as the value of the file.resource.loader.path property. I also added the 'runtime.log.logsystem.class' property, and set it because I was getting exceptions complaining about logging.

UTILTY VELOCITY METHODS

public static VelocityEngine  getVelocityEngine(){

    VelocityEngine ve = new VelocityEngine();
    Properties props = new Properties();
    // THIS PATH CAN BE HARDCODED BUT IDEALLY YOUD GET IT FROM A PROPERTIES FILE
    String path = "/absolute/path/to/templates/dir/on/your/machine";
    props.put("file.resource.loader.path", path);
    props.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem");
    ve.init(props);
    return ve;
}

public static String getHtmlByTemplateAndContext(String templateName, VelocityContext context){

    VelocityEngine ve = getVelocityEngine();

    Template template = ve.getTemplate(templateName);

      StringWriter writer = new StringWriter();
      template.merge(context, writer );
      System.out.println( writer.toString());
      String velocityHtml = writer.toString();
      return velocityHtml;
}

HOW TO USE ABOVE CODE, CREATING A VELOCITY CONTEXT TO FEED TO UTILTY METHOD

Here is how you can use the above methods. You simply specify the filename of the template file, and create a simple VelocityContext context to store your template variables.

        VelocityContext context = new VelocityContext();
        context.put("lastName", "Mavis");
        context.put("firstName", "Roger");
        context.put("email", "[email protected]");
        context.put("title", "Mr.");

        String html =    VelocityUtil.getHtmlByTemplateAndContext("email_template_2015_10_09.vm", context);

EXAMPLE TEMPLATE HTML

The variables can be accessed like (in my case saved in file email_template_2015_10_09.vm):

<p> Hello $firstName $lastName </p>
like image 29
med116 Avatar answered Sep 22 '22 21:09

med116