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
Newsletter
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
  • The Code-Volume Delusion: Rethinking Engineering Velocity in the AI Era
  • Code and Connect: MCP + MuleSoft
  • MuleSoft MCP and A2A in Production: What 17 Recipes Reveal

Trending

  • Common REST Design Pattern
  • Introducing senro: A Pipeline Engine You Program in Go
  • AI Coding Agent Guardrails: Finding Gaps in Tool Permissions
  • Nobody Designs an RBAC Mess; Everyone Ends Up With One

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Mar. 08, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
9.5K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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
  • The Code-Volume Delusion: Rethinking Engineering Velocity in the AI Era
  • Code and Connect: MCP + MuleSoft
  • MuleSoft MCP and A2A in Production: What 17 Recipes Reveal

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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