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.
Let’s first create 2 html templates file in our Mule application under src/main/resource folder.
Let the first file be template1.html:
Let the next file be template2.html:
Now, let’s create the Mule flow that will load these 2 html file template dynamically based on our request:
As 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:
This 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:
We will pass the header with file name as template1.html :
As we can clearly see here its loading template1 as output.
We will now pass the file name as template2.html as follows:
We can see its loading template2 now!
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.