---
schema_version: "1"
id: e66f60b4548e03f9f9e03ebc0cfc53ea2d4ebcfd4565e9b6058c312ed38cfd91
title: Parse Template Reference
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/mule-runtime/4.6/parse-template-reference.md
canonical_url: https://docs.mulesoft.com/mule-runtime/4.6/parse-template-reference
source_site: docs.mulesoft.com
content_type: documentation
content_hash: fcf2f8903696147fc097c72c6cf94e15f2ad0fc6458fc9c3cf585e32800563ac
description: MuleSoft Documentation Site
release: "4.6"
latest: false
---
---
product: Mule Runtime
version: 4.6
keywords: anypoint studio, parse
---

# Parse Template Reference

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

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.

[Examples](#code_example)

## 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](/mulesoft/current/en-us/mule-runtime/4.6/_images/parse.png)

    <table><colgroup><col> <col> <col></colgroup><thead><tr><th>Field</th><th>Value</th><th>Description</th></tr></thead><tbody><tr><td><div><div><p><strong>Content</strong></p></div></div></td><td><div><div><p>Template String</p></div></div></td><td><div><div><p>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.</p></div></div></td></tr><tr><td><div><div><p><strong>Display Name</strong></p></div></div></td><td><div><div><p>Parse Template</p></div></div></td><td><div><div><p>Customize to display a unique name for the transformer in your application.</p></div></div></td></tr><tr><td><div><div><p><strong>Location</strong></p></div></div></td><td><div><div><p>filepath</p></div></div></td><td><div><div><p>Define the location of the file that Mule uses as a template into which to insert values extracted from the message properties or variables.</p></div></div></td></tr><tr><td><div><div><p><strong>Target Variable</strong></p></div></div></td><td><div><div><p>Variable Name</p></div></div></td><td><div><div><p>The name of a variable that stores the result of evaluating the expression defined in Target Value.</p></div></div></td></tr><tr><td><div><div><p><strong>Target Value</strong></p></div></div></td><td><div><div><p>Expression</p></div></div></td><td><div><div><p>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.</p></div></div></td></tr></tbody></table>

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.

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

Attributes of the `parse-template` element:

<table><colgroup><col> <col></colgroup><thead><tr><th>Element Attributes</th><th>Value</th></tr></thead><tbody><tr><td><div><div><p><code>content</code></p></div></div></td><td><div><div><p>A string representing the template to be used where the embedded expressions will be evaluated and replaced by their results.</p></div></div></td></tr><tr><td><div><div><p><code>location</code></p></div></div></td><td><div><div><p>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.</p></div></div></td></tr><tr><td><div><div><p><code>doc:name</code></p></div></div></td><td><div><div><p>Customize to display a unique name for the transformer in your application. (Note: Not needed in Mule standalone.)</p></div></div></td></tr><tr><td><div><div><p><code>target</code></p></div></div></td><td><div><div><p>The name of a variable where the result of the expression defined in <code>targetValue</code> will be stored after the Parse Template is executed.</p></div></div></td></tr><tr><td><div><div><p><code>targetValue</code></p></div></div></td><td><div><div><p>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 <code>target</code> attribute.</p></div></div></td></tr></tbody></table>

## Examples

The following examples use Parse Template to load data dynamically into HTML:

- [Processing Results of a Database Query Dynamically](#ex_1)

- [Dynamically Generating Data in HTML Rows](#ex_2)

### Processing Results of a Database Query Dynamically

This example uses Parse Template in a Mule application that loads employee data from a database into
an HTML template. The flow returns the template-built output to the caller.

![parse template flow](/mulesoft/current/en-us/mule-runtime/4.6/_images/parse-template-flow.png)

#### Input to Parse Template

Assume that you start the flow with a URI that contains a query parameter that identifies the ID of
an employee, such as `http://localhost:8081/getEmployee?id=1234`, and that the Select operation
returns the following input to Parse Template:

```html
{ "first_name": "Melba", "last_name": "Roy Mouton", "department": "Computers", "job_title":
"Assistant Chief of Research Programs", "start_date": "01/01/2021", "employee_type": "mathematician"
}
```

#### Template Content

The template selects fields containing name, department, job title, start date, and employee type
values from the input payload.

```html
<html>
  <body>
    <table>
      <thead>
        <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>
      </thead>
      <tbody>
        <tr>
          <td>#[payload['first_name']]</td>
          <td>#[payload['last_name']]</td>
          <td>#[payload['department']]</td>
          <td>#[payload['job_title']]</td>
          <td>#[payload['start_date']]</td>
          <td>#[payload['employee_type']]</td>
        </tr>
      </tbody>
    </table>
  </body>
</html>
```

#### Configuration XML

In the Mule configuration XML for the example, Parse Template loads the template file through the
setting `location="src/main/resources/responseHtml.template"`.

```xml
<?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" >
        <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">
        <http:listener-connection host="localhost" port="8081" />
    </http:listener-config>
    <flow name="exampleTemplateFlow1" >
        <http:listener doc:name="Listener" config-ref="HTTP_Listener_config"
          path="/getEmployee"/>
        <db:select doc:name="Select" 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"
          location="src/main/resources/responseHtml.template"/>
    </flow>
</mule>
```

#### Output of the Template

The [example](#template) returns template-built output. The output is HTML with the selected
employee data.

```xml
<html>
  <body>
    <table>
      <thead>
        <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>
      </thead>
      <tbody>
        <tr>
          <td>"Melba"</td>
          <td>"Roy Mouton"</td>
          <td>"Computers"</td>
          <td>"Assistant Chief of Research Programs"</td>
          <td>"01/01/2021"</td>
          <td>"mathematician"</td>
        </tr>
      </tbody>
    </table>
  </body>
</html>
```

### Dynamically Generating Data in HTML Rows

The example uses Parse Template on employee records that are represented as an array of objects.

![parse template flow2](/mulesoft/current/en-us/mule-runtime/4.6/_images/parse-template-flow2.png)

#### Input to Parse Template

Assume that Parse Template component receives the following payload from Set Payload:

```html
{ Employee: [ { "first_name" : "Melba", "last_name" : "Roy Mouton", "department" : "Computers",
"job_title" : "Assistant Chief of Research Programs", "start_date" : "01/01/2021", "employee_type" :
"mathematician" }, { "first_name" : "Annie", "last_name" : "Easley", "department" : "Software
Development", "job_title" : "Technical Lead", "start_date" : "06/02/2020", "employee_type" : "Rocket
Scientist" } ] }
```

#### Template Content

The following template uses a DataWeave script to iterate over the input array and construct an HTML
row for each object in the array:

```html
<html>
  <body>
    <table>
      <thead>
        <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>
      </thead>
      <tbody>
        #[%dw 2.0 output application/java --- payload.*Employee[0] map ((item) -> "
        <tr>
          <td><span>" ++ (item.first_name default "") ++ "</span></td>
          <td><span>" ++ (item.last_name default "") ++ "</span></td>
          <td><span>" ++ (item.department default "") ++ "</span></td>
          <td><span>" ++ (item.job_title default "") ++ "</span></td>
          <td><span>" ++ (item.start_date default "") ++ "</span></td>
          <td><span>" ++ (item.employee_type default "") ++ "</span></td>
        </tr>
        ") joinBy ""]
      </tbody>
    </table>
  </body>
</html>
```

#### Configuration XML

In the Mule configuration XML, Parse Template loads the template file through the setting,
`location="/path/to/my.template"`.

```xml
<?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="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/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">
	<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener
   config" >
		<http:listener-connection host="0.0.0.0" port="8081" />
	</http:listener-config>
	<flow name="exampleTemplateFlow" >
		<http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="/parseit"/>
		<set-payload doc:name="Set Payload" value='#[{
	Employee: [

			{
				"first_name" : "Melba",
				"last_name" : "Roy Mouton",
				"department" : "Computers",
				"job_title" : "Assistant Chief of Research Programs",
				"start_date" : "01/01/2021",
				"employee_type" : "mathematician"

			},
			{
				"first_name" : "Annie",
				"last_name" : "Easley",
				"department" : "Software Development",
				"job_title" : "Technical Lead",
				"start_date" : "06/02/2020",
				"employee_type" : "Rocket Scientist"

			}

	]
}]'/>
		<parse-template doc:name="Parse Template" location="/path/to/my.template">
		</parse-template>
		<logger level="INFO" doc:name="Logger" message="#[payload]"/>
	</flow>
</mule>
```

#### Output of the Template

The [example](#template2) returns template-built output. The output is HTML with the dynamically
produced rows and data from the objects in the array.

```xml
<html>
  <body>
    <table>
      <thead>
        <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>
      </thead>
      <tbody>
        <tr>
          <td><span>Melba</span></td>
          <td><span>Roy Mouton</span></td>
          <td><span>Computers</span></td>
          <td><span>Assistant Chief of Research Programs</span></td>
          <td><span>01/01/2021</span></td>
          <td><span>mathematician</span></td>
        </tr>
        <tr>
          <td><span>Annie</span></td>
          <td><span>Easley</span></td>
          <td><span>Software Development</span></td>
          <td><span>Technical Lead</span></td>
          <td><span>06/02/2020</span></td>
          <td><span>Rocket Scientist</span></td>
        </tr>
      </tbody>
    </table>
  </body>
</html>
```

## 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:

  ```XML
  <td>#['hello #[upper("world")]']</td>
  <td>#['hello ' ++ upper("world")]</td>
  <td>#['hello upper("world")']</td>
  <td>#['hello ++ upper("world")']</td>
  ```

  Output Values:

  ```XML
  <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:

  ```XML
  <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:

  ```XML
  <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>
  ```

  For more information, see
  [Escape Special Characters](https://docs.mulesoft.com/dataweave/latest/dataweave-language-introduction#escape-special-characters).

## See Also

[Set Variable Transformer](https://docs.mulesoft.com/mule-runtime/4.6/variable-transformer-reference)
