A Technologist Who Speaks Business
I was surprised to discover today that, out of the box, it’s difficult to include one Velocity template from another if you are using Apache Camel. For once, a search of the Internet didn’t easily find the solution.
So, for the benefit of others, here is the solution….
Say you have an email Velocity template like this named “notification-email.vm”:
<html> <body> Hello from Molly corp. Please buy our stuff! This has been a message from Molly Corp. If you want to unsubscribe, <a href="http://www.mollycorp.com/ignore-unsubscribe.jsp">click here</a> </body> </html>
And let’s say that notification-email.vm is on your classpath (maybe in a jar or something)
You can have Camel send that email with a simple:
So far, so good. This will work out of the box. But let’s say you want to take the reasonable step of pulling that common footer out into a common file and including (with a #include or a #parse) it instead from a file named “footer.vm” that is also in your classpath right next to notification-email.vm, like this:
<html> <body> Hello from Molly corp. Please buy our stuff! #include("footer.vm") </body> </html>
This will not work! To get it to work, you have to use a custom properties file and refer to it from Camel. First, create a file named “velocity_custom.properties” like this:
resource.loader = file, class class.resource.loader.description = Velocity Classpath Resource Loader class.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
This will add a second resource loader that searches the classpath in addition to the default one that searches the file system. Next, put that properties file in your classpath and change your velocity route like this:
Magic! Now it works.♦ End