Mule ESB: Using Static Templates to Produce Dynamic Content

January 29, 2015
blog author

Appno Blogger

Appnovation Coop

I am all about using the tools available to me to make my job easier and to not reinvent the wheel. There is an often ignored transformer in Mule ESB that is very handy for creating static content/templates with the ability to add dynamic content through the MEL (Mule Expression Language). What is this mystery transformer? It is the "parse-template" transformer. Chances are that if you haven't used this transformer before, but are an AnypointStudio user, you have seen this transformer listed in the Transformers list. How is this transformer useful? If you have used this transformer before, you already know why it's very handy in certain scenarios. I have mostly used this transformer for email templates, but it can be useful in other situations, such as returning HTML to a client from an HTTP transport. How do I use this transformer? It's fairly straightforward. Use the tag shown below to point to your template, and what the transformer does is basically set the payload to the result of your template being parsed. <parse-template location="src/test/resources/emailTemplate.txt" doc:name="Parse Template"/> What does a template look like? For a template used for sending an email, it could look something like this: Hello #[], You have just submitted file: #[payload.fileName]. Have a great day. For a template used for returning some HTML content, it could look something like this: <html> <body> <h3>You just submitted a file (#[payload.fileName]) on behalf of:</h3> Name: #[payload.firstName], Last Name: #[payload.lastName] </body> </html> What else do I need to know? This topic is not very complicated, and this article should have provided you with enough information to use the parse-template transformer. However, if you would like to view the Mule ESB documentation on this topic, please see: Mule ESB Documentation