HTML Template Processing Workflow

An HTML template is HTML markup containing inline expressions such as {{FirstName}} or {{Address.City}}. Aspose.HTML for .NET binds these expressions to XML or JSON data and generates a populated HTML document through Converter.ConvertTemplate() .

Use this API workflow for server-side reports, messages, tables, catalog pages, and other documents whose structure remains stable while the data changes. It is template processing inside a C# application, not an online file converter.

Install Aspose.HTML for .NET from NuGet before running the template-processing example:


Install Aspose.HTML for .NET

Install-Package Aspose.HTML

Prepare XML or JSON Template Data

Create a TemplateData instance from an XML or JSON file, or supply inline content with the appropriate content type. The following XML source contains two Person records used by the template below.


XML Data Source

<Data>
    <Persons>
        <Person>
            <FirstName>Sherlock</FirstName>
			<LastName>Doe</LastName>
			<Address>
				<City>Dallas</City>
				<Street>Main rd.</Street>
				<Number>114</Number>
			</Address>
			<Telephone>012-5344-334</Telephone>
        </Person>
        <Person>
            <FirstName>Jack</FirstName>
            <LastName>Fox</LastName>
            <Address>
                <Number>25</Number>
                <Street>Broadway</Street>
                <City>New York</City>
            </Address>
            <Telephone>081-544-12-15</Telephone>
        </Person>
    </Persons>
 </Data>

Create the HTML Template Markup

Template markup combines normal HTML with expressions that select values or repeat markup for data collections. The essential rules are:

  • A data-binding expression such as {{FirstName}} or {{Address.Street}} inserts a value selected from the current data object.
  • The data_merge attribute marks the element that should be repeated for a collection.
  • The #foreach directive selects the collection used when repeating the marked element.

XML Data Source Example

<!DOCTYPE html>
<html lang="en">
	<head>
		<title>Person Information</title>
	</head>
	<body>
		<table border=1 data_merge="{{#foreach Persons.Person}}">
			<tr>
				<th>Person</th>
				<th>Address</th>
				<th>Telephone</th>
			</tr>
			<tr>
				<td>{{FirstName}} {{LastName}}</td>
				<td>{{Address.Street}} {{Address.Number}}, {{Address.City}}</td>
				<td>{{Telephone}} </td>
			</tr>
		</table>
	</body>
</html>

Populate an HTML Template in C#

After preparing the template and data source, call Converter.ConvertTemplate() to replace the expressions and write the populated HTML. The example uses the ConvertTemplate(HTMLDocument, TemplateData, TemplateLoadOptions, string) overload:

  • HTMLDocument contains the source template.
  • TemplateData contains the XML or JSON values.
  • TemplateLoadOptions controls template processing, including whether names are matched regardless of case.
  • The output path identifies the generated HTML file.

C# Code to Bind XML Data to an HTML Template

using Aspose.Html;
using Aspose.Html.Converters;
using Aspose.Html.Loading;

// Load the HTML template and XML data
using HTMLDocument template = new HTMLDocument("html-template.html");
TemplateData data = new TemplateData("template-data.xml");

// Configure template processing
TemplateLoadOptions options = new TemplateLoadOptions();

// Populate the template and write the generated HTML file
Converter.ConvertTemplate(template, data, options, "document.html");

The generated HTML contains one populated table for each Person in the XML data:

HTML table filled with data using the template

Converter.ConvertTemplate() also accepts a file path, URL, or inline HTML source. Overloads without an output path return an HTMLDocument, which can be inspected, modified, saved, or rendered in a later step.

HTML Template Processing Notes

  • XML and JSON field names must match the expressions. Configure TemplateLoadOptions.NamesAreCaseSensitive when the source naming convention requires case-insensitive matching.
  • A base URL matters for inline templates. Supply it when the markup contains relative CSS, images, fonts, or links.
  • Escaping and output context need testing. Verify generated text and attribute values with representative input, especially when data is not fully controlled by the application.
  • Template processing produces HTML. PDF, DOCX, or image output is a separate rendering step performed after the populated HTMLDocument is created.
  • Validate empty and repeated data. Test missing properties, empty collections, nested objects, and optional attributes before production use.

Read Convert Template to HTML for file-based and inline C# examples. See Setting Attributes in an HTML Template for conditional attributes and TemplateContentOptions examples.