Getting dynamic templates in Mule

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Mule has a feature of loading external files in the application using it’s  parse template component which loads the content of a flow-external file into a Mule flow, then use the file as a template with which we can insert data returned as the result of evaluated Mule expressions.

We can use different types of file in our Mule application. In this blog, we will try to load the external File templates dynamically into our Mule application.

Let’s first create 2 html templates file in our Mule application under src/main/resource folder.

Let the first file be template1.html:

Untitled

Let the next file be template2.html:

Untitled

Now, let’s create the Mule flow that will load these 2 html file template dynamically based on our request:

UntitledAs we can see we are trying to load our html template file at the end of the flow using a custom transformer which expects a dynamic value as its property and that can be anything; in this case, we are using an HTTP header.

The Java class we used here is as follows:

UntitledThis Java class will get the file name as MEL expression and will fetch that file from classpath.

Testing our application:

We will now deploy the application in Mule runtime and test our application hitting the url as follows:

case1:

We will pass the header with file name as template1.html :

Untitled

As we can clearly see here its loading template1 as output.

case2:

We will now pass the file name as template2.html as follows:

Untitled

We can see its loading template2 now!

Conclusion

As we can see we can load the content of the file in our application dynamically as an output. We can use variable, properties or thing dynamically to load the file at runtime.

Hope you enjoyed the article and it was helpful to you.

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Anirban Sen Chowdhary

Anirban Sen Chowdhary is an information technology professional currently working on Java/J2ee, Esb and Integration platform. For more information, please visit http://anirbansenchowdhary.com and you can also follow https://twitter.com/Anir37

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>