cancel
Showing results for 
Search instead for 
Did you mean: 

How to create global macros for velocity email templates.

Former Member
0 Kudos

Good Day,

I'm trying to create a global velocity macro for usage in email templates in hybris5. I have created an VM_global_libraries.vm, where i declare my velocity macros in, however when i try to use in my emails it doesn't seem to locate it.

Macro Definition in VM_global_libraries

#macro(utc_date $name) Hello, $name #end

Usage in email templates <td> #utc_date("jeffery") </td>

Result #utc_date("jeffery")

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

Per default the VM_global_library.vm file does not get parsed, since the property velocimacro.library is commented out within the bin/platform/ext/core/lib/velocity-1.7.jar!/org/apache/velocity/runtime/defaults/velocity.properties. Currently there is no way to change the properties without using a customized jar. But there is a workaround you just need to extend the VelocityTemplateRenderer and override the evaluate method, this way you can set custom properties before the Velocity Engine gets initialized.

 public class CustomVelocityTemplateRenderer extends VelocityTemplateRenderer
 {
 
     private Properties properties;
 
     @Override
     protected void evaluate(Writer result, VelocityContext ctx, Reader reader) throws IOException
     {
         if (properties != null)
         {
             Velocity.init(properties);
         }
         super.evaluate(result, ctx, reader);
     }
 
     @Required
     public void setProperties(Properties properties)
     {
         this.properties = properties;
     }
 }

In my example I just injected a properties file via Spring, this way you can set the path to your global velocity macro template via the property velocimacro.library e.g. velocimacro.library=VM_global_library.vm. This way you can define other properties too without touching your code, to get the #parse function working via classpaths instead of absolute file paths you just need to define another resource loader:

 resource.loader = class    
 class.resource.loader.description = Velocity Classpath Resource Loader
 class.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader

Now you can use #parse("path/to/your/custommacro.vm") to parse macros that you don't want to define within your global library for example.

Former Member
0 Kudos

Thanx Thomas, not sure how i would do this in hybris management console (hmc). We currently add our template files to the Media objects in hmc, which places it in the 'root' folder. I have added the VM_global_library.vm to the Media object and it too is stored in root folder. Unfortunately it still does not get it.

Former Member
0 Kudos

I see. The root folder that Velocity sees might not be the one that you think it should see.

As a simple workaround, you could try to parse the macro file yourself at the top of the template. Like so:

 #parse("VM_global_library.vm")

You may need to experiment with the path to the file.

Former Member
0 Kudos

The file name is VM_global_library.vm. It should be placed in the root of the template path.