Contact Us 1-800-596-4880

Parse Template Reference

Standard Support for Mule 4.1 ended on November 2, 2020, and this version of Mule reached its End of Life on November 2, 2022, when Extended Support ended.

Deployments of new applications to CloudHub that use this version of Mule are no longer allowed. Only in-place updates to applications are permitted.

MuleSoft recommends that you upgrade to the latest version of Mule 4 that is in Standard Support so that your applications run with the latest fixes and security enhancements.

Parse Template is the Mule component to use for processing a template and obtaining a result. A template is defined as text with embedded Mule expressions that are evaluated and replaced with their result.

You can configure template through an external file reference, or you can embed it in the component definition itself.

Configuring a Parse Template

  1. In Studio, drag a Parse Template message processor from the palette onto the canvas.

  2. Configure the fields described in this table:

    parse
    Field Value Description

    Content

    Template String

    A string to use as a template. Instead of defining the template in an external file, you can use this field to write it inline. It can contain embedded expressions to be evaluated and replaced with their results.

    Display Name

    Parse Template

    Customize to display a unique name for the transformer in your application.

    Location

    filepath

    Define the location of the file that Mule uses as a template into which to insert values extracted from the message properties or variables.

    Target Variable

    Variable Name

    The name of a variable that stores the result of evaluating the expression defined in Target Value.

    Target Value

    Expression

    Mule Expression to be evaluated after executing Parse Template. The result of this expression is stored in a variable with the name defined in the Target Variable field.

If you are using the XML editor in Studio or a Standalone Mule instance:

  • Add a parse-template element to your flow, then configure it according to the tables below.

    <parse-template location="users/administrator/desktop/hrweb/confirmation.html" doc:name="Parse Template"/>

Attributes of the parse-template element:

Element Attributes Value

content

A string representing the template to be used where the embedded expressions will be evaluated and replaced by their results.

location

Filepath which defines the location of the file that Mule uses as a template into which to insert values extracted from the message properties or variables.

doc:name

Customize to display a unique name for the transformer in your application. (Note: Not needed in Mule standalone.)

target

The name of a variable where the result of the expression defined in targetValue will be stored after the Parse Template is executed.

targetValue

A Mule Expression that will be evaluated after the Parse Template is executed and which result will be stored in a variable with name as defined in the target attribute.

Code Example

The following example demonstrates the use of a Parse Template in an application that accepts queries by employeeID to acquire data about an employee.

In this case, Parse Template uses a file external to the flow as a template into which it inserts values for fields (name, department, job title, start date, and employee type) drawn from the message payload. The flow then returns the template-built payload to the caller.

parse template flow
<html>
	<body>
		<table>
		<tr>
			<th>First Name</th>
			<th>Last Name</th>
			<th>Department</th>
			<th>Job Title</th>
			<th>Start Date</th>
			<th>Employee Type</th>
		</tr>

		<tr>
			<td>#[payload[0]['first_name']]</td>
			<td>#[payload[0]['last_name']]</td>
			<td>#[payload[0]['department']]</td>
			<td>#[payload[0]['job_title']]</td>
			<td>#[payload[0]['start_date']]</td>
			<td>#[payload[0]['employee_type']]</td>
		</tr>
		</table>

	</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:http="http://www.mulesoft.org/schema/mule/http"
      xmlns:db="http://www.mulesoft.org/schema/mule/db"
      xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
    <db:config name="Database_Config" doc:name="Database Config" doc:id="e4df87d8-ec1e-49ec-a528-43e93baa5bda" >
        <db:my-sql-connection host="localhost" port="3306" user="user" password="pw" database="MySQL_Data_Source" />
    </db:config>
    <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="724690b1-cc2f-451a-86eb-66f7750feba1">
        <http:listener-connection host="localhost" port="8081" />
    </http:listener-config>
    <flow name="exampleTemplateFlow1" doc:id="b8cc9464-5991-4977-978f-9f51eb252ec8" >
        <http:listener doc:name="Listener" doc:id="f334ba27-ae1f-4da5-a194-26e57a1aa637" config-ref="HTTP_Listener_config" path="/getEmployee"/>
        <db:select doc:name="Select" doc:id="c3ab7830-9920-4637-9329-954a8a94e54c" config-ref="Database_Config">
            <ee:repeatable-file-store-iterable />
            <db:sql >SELECT * FROM Employees WHERE id=#[attributes.queryParams['id']]</db:sql>
        </db:select>
        <parse-template doc:name="Parse Template" doc:id="53211517-f943-40b2-86e3-20a0e1ca4eb6" location="src/main/resources/responseHtml.template"/>
    </flow>
</mule>

To use the application in this example, you must send an HTTP request with a URL that includes an ID query parameter, such as http://localhost:8081/getEmployee?id=12345.

Special Characters

Parse Template supports the use of expressions within a template and the use of literals within these expressions. The template recognizes certain special characters, such as the sequence \#[ and quotations marks. You can escape such characters to treat them as regular characters.

  • #[an-expression]:

    Within a template, use the characters #[] to designate an expression. For example, a template can contain expressions within HTML elements, such as <td>#[upper("mulesoft")]</td>, which resolves to <td>MULESOFT</td>. Empty expressions (#[]) return an error, but empty strings, such as #[''] or #[""] are valid expressions.

  • Sub-expressions:

    To resolve a sub-expression that is surrounded by quotation marks, surround the sub-expression with another #[]. For example, the four elements in the template snippet below contain the same sub-expression (upper("world")), either inside or outside of quotation marks. The first two return the same results. The last two do not process upper("world") because the sub-expression is inside the quotation marks but not surrounded by another #[].

    Parse Template Snippet:
    <td>#['hello #[upper("world")]']</td>
    <td>#['hello ' ++ upper("world")]</td>
    <td>#['hello upper("world")']</td>
    <td>#['hello ++ upper("world")']</td>
    Output Values:
    <td>hello WORLD</td>
    <td>hello WORLD</td>
    <td>hello upper("world")</td>
    <td>hello ++ upper("world")</td>
  • Escape character (\):

    Parse Template uses the character sequence #[ to identify where an expression begins. To avoid this behavior and treat that sequence as literal characters, escape it with \. For example, <td>#[</td> returns <td>#[</td>.

    In addition, expressions can contain strings with special characters that you want to treat as regular characters. To escape any special characters within a string that is embedded inside an expression, append \ to the character. Examples of special characters include the sequence #[, quotations marks (' or "), apostrophes ('), and $. It is not necessary to escape \# or [ unless they are adjacent to one another in the string, with the \# preceding the [.

    Parse Template Snippet:
    <td>\#[</td>
    <td>#['abcd\#[-1234' ++ now() as String ++ '.log']</td>
    <td>'abc'def'</td>
    <td>#['abc\'def']</td>
    <td>"xyz"xyz"</td>
    <td>#["xyz\"xyz"]</td>
    <td>#["abc\$DEF\#ghi\[JKL]"]</td>
    Output Values:
    <td>#[</td>
    <td>abcd#[-12342020-07-06T17:20:10.683-07:00.log</td>
    <td>'abc'def'</td>
    <td>abc'def</td>
    <td>"xyz"xyz"</td>
    <td>xyz"xyz</td>
    <td>abc$DEF#ghi[JKL]</td>