DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • MuleSoft: Tactical and Strategical Role of an Application Template
  • MuleSoft MCP and A2A in Production: What 17 Recipes Reveal
  • MuleSoft IDP: Enhancing Efficiency and Accuracy in Data Extraction
  • From AI Chaos to Control: Building Enterprise-Grade LLM Gateways With MuleSoft Anypoint

Trending

  • Building a High-Throughput Distributed Sequence Generator Using the Hi-Lo Algorithm
  • Testing AI-Infused Apps: A Dual-Layer Framework for AI Quality Assurance
  • How to Save Money Using Custom LLMs for Specific Tasks
  • Building a Spring AI Assistant With MCP Servers: A Step-by-Step Tutorial
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. MuleSoft : Dynamic Content (Parser/Velocity Template)

MuleSoft : Dynamic Content (Parser/Velocity Template)

In this post, we will discuss, and provide examples of, how to use MuleSoft's Velocity Template and its Parse Template component.

By 
Amit Ghorpade user avatar
Amit Ghorpade
·
Mar. 08, 18 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
9.3K Views

Join the DZone community and get the full member experience.

Join For Free

While working with some Spring applications, I came across Velocity Template, which is used to dynamically replace pieces of content in emails.

Some common use cases for the Velocity Template are welcome, registration, and password reset emails, where the developer just needs to replace the user’s email id, name, or reset password token. In this post, we will go through how to use Velocity Template and its magical component, Parse Template.

Velocity Template

Velocity Engine reads templates from given a resource, or LoaderPath. Add the below Velocity engine bean dependency to your code:

<beans:beans>
<beans:bean name=”velocityEngine” class=”org.springframework.ui.velocity.VelocityEngineFactoryBean”>
<beans:property name=”resourceLoaderPath” value=”classpath:templates”/>
</beans:bean>
</beans:beans>

This Velocity Engine will read template files from the resource/templates folder in your application.

Now, we will add a custom Java class which will have a bean property like the above Velocity engine, and another class which will create the template file name.

<custom-transformer class=”emailsendproject.VelocityMessageTransformer” doc:name=”Java”>
<spring:property name=”velocityEngine” ref=”velocityEngine” />
<spring:property name=”templateName” value=”WelcomeEmail.vm” />
</custom-transformer>

Email Template:

Welcome ${firstname} 
 This is send email with velocity template example demo.
Regards,
Amit Ghorpade

In this case, ${firstname } will be replaced by the dynamic value in the custom Java class using the Velocity template engine.

MuleSoft's Mule flow will look as shown below. In this flow, we will get input parameters from an HTTP component, then we will call a custom Java class where the Velocity engine will replace static content with input values and return the payload which will be used in the email's body content.

muleflow

Parse Template 

If you don’t know much more about the Velocity template and still want to achieve dynamic content templating, MuleSoft came up with a great component - Parse Template.

Parse Template accepts template file locations which will have content similar to what I've depicted below: 

Hello #[payload.firstname],#[message.inboundProperties.username]
    You have just submitted file: #[payload.fileName].
   Have a great day.
 Regards,
#[flowVars.sender]

Before we call this Parse Template component, set the values in payload, flowVars, andinboundproperties.

I loved this Parse Template feature. You can test both the examples with the code I've provided on my GitHub repo.

I hope this will help you in your project.

Template MuleSoft Velocity (JavaScript library)

Published at DZone with permission of Amit Ghorpade. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • MuleSoft: Tactical and Strategical Role of an Application Template
  • MuleSoft MCP and A2A in Production: What 17 Recipes Reveal
  • MuleSoft IDP: Enhancing Efficiency and Accuracy in Data Extraction
  • From AI Chaos to Control: Building Enterprise-Grade LLM Gateways With MuleSoft Anypoint

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook