on 07-17-2014 5:08 PM
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")
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The file name is VM_global_library.vm. It should be placed in the root of the template path.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
6 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.