HTML Template - C# Examples

This article describes how to create an HTML document based on a template and populate it from a data source. Aspose.HTML provides the inline expressions syntax to work with templates and various data source types, such as XML and JSON.

Template markup

The HTML template is a regular HTML file that contains some special inline expressions that specify the input data-source mapping to the HTML page markup. These inline expressions use double curled bracket notation. The template markers will be replaced with respective data values during the template processing following the rules described below.

Inline expressions

Following is the list of supported inline expressions syntax.

{{ … }} - data-binding expression

The data-binding expression is used to set values of the control element based on the information that is contained in the data source.

The following is the basic syntax of the data-binding expression:

{{ data-binding expression }}

The following demo shows how to use the data-binding expression to obtain information from the XML data source:

XML Data Source

2    <FirstName>John</FirstName>
3    <LastName>Doe</LastName>
4    <Address>
5        <City>Dallas</City>
6        <Street>Austin rd.</Street>
7        <Number>200</Number>
8    </Address>

The data-binding expression in the HTML page template:

HTML Template

This is an example of an HTML template that uses inline expressions to bind data. The template includes a table with two columns for “Person” and “Address”. The cells in the “Person” column display the values of {{FirstName}} and {{LastName}} from the data source. The cells in the “Address” column display the values of {{Address.Street}} {{Address.Number}} and {{Address.City}}. The curly-brace syntax is used to indicate an inline expression.

 1<table border=1>
 2    <tr>
 3        <th>Person</th>
 4        <th>Address</th>
 5    </tr>
 6    <tr>
 7        <td>{{FirstName}} {{LastName}}</td>
 8        <td>{{Address.Street}} {{Address.Number}}, {{Address.City}}</td>
 9    </tr>

{{#foreach … }} - foreach directive expression

The foreach directive expression is used to iterate through the list of elements in combination with data-binding expression.

The following demo shows how to get all persons from an XML data source and populate a template:

XML Data Source

 1 <Data>
 2     <Persons>
 3         <Person>
 4             <FirstName>John</FirstName>
 5 			<LastName>Doe</LastName>
 6             <Address>
 7                 <Number>200</Number>
 8                 <Street>Austin rd.</Street>
 9                 <City>Dallas</City>
10             </Address>
11             <Phone1>345-345-34-55</Phone1>
12             <Phone2>345-555-09-09</Phone2>
13         </Person>
14         <Person>
15             <FirstName>Jack</FirstName>
16             <LastName>Fox</LastName>
17             <Address>
18                 <Number>25</Number>
19                 <Street>Broadway</Street>
20                 <City>New York</City>
21             </Address>
22             <Phone1>081-544-12-15</Phone1>
23         </Person>
24         <Person>
25             <FirstName>Sherlock</FirstName>
26             <LastName>Holmes</LastName>
27             <Address>
28                   <Number>65</Number>
29                   <Street>Baker str.</Street>
30                   <City>London</City>
31             </Address>
32             <Phone1>012-5344-334</Phone1>
33         </Person>
34     </Persons>
35 </Data>

The foreach directive expression in the HTML page template:

HTML Template

The data_merge attribute specifies the data source is a list of objects and the table should be repeated for each object in the list. The expressions inside the template, such as {{FirstName}}, {{LastName}}, {{Address.Street}}, and {{Address.Number}}, indicate the fields from the data source that should be inserted in the corresponding cells of the table.

 1 <table border=1 data_merge='{{#foreach Persons.Person}}'>
 2     <tr>    
 3         <th>Person</th>
 4         <th>Address</th>
 5     </tr>
 6     <tr>
 7         <td>{{FirstName}} {{LastName}}</td>
 8         <td>{{Address.Street}} {{Address.Number}}, {{Address.City}}</td>
 9     </tr>
10 </table>

Please note that the current implementation only supports foreach directive expression for the following HTML elements: DIV, OL, UL and TABLE.

Data Source

As it was mentioned earlier, the data source could be represented in XML and JSON formats. The following are the examples of both data sources that are prepared for this article:

XML Data Source

1 <Data>
2     <FirstName>John</FirstName>\
3     <LastName>Doe</LastName>\
4     <Address>\
5         <City>Dallas</City>\
6         <Street>Austin rd.</Street>\
7         <Number>200</Number>\
8     </Address>
9 </Data>

JSON Data Source

1 {
2     'FirstName': 'John',\
3     'LastName': 'Doe',\
4     'Address': {\
5         'City': 'Dallas',\
6         'Street': 'Austin rd.',\
7         'Number': '200'
8     }
9 }

Convert Template to HTML by a single line of code

Once you have prepared an HTML Template, you can convert Template to HTML in your C# application literally with a single line of code! To make this, you need to pass the required parameters to the ConvertTemplate() (sourcePath, data, options, outputPath) method.

 1using System.IO;
 2using Aspose.Html.Converters;
 3using Aspose.Html.Loading;
 5    // Convert template to HTML
 6    Converter.ConvertTemplate(
 7        Path.Combine(DataDir, "template.html"),
 8        new TemplateData(Path.Combine(DataDir, "data-source.json")),
 9        new TemplateLoadOptions(),
10        Path.Combine(OutputDir, "template-with-single-line.html")
11    );

The above code snippet shows how to use the ConvertTemplate() method to create an HTML document based on a template and populate it with data from a JSON data source. The method takes four arguments: the path to the template file, the data source, the loading options for the template, and the path where the output HTML document should be saved. The resulting document will be populated with the data from the data source, using the inline expressions syntax to fill in the template.

For more information on how to convert template to HTML, please visit the Convert Template to HTML article.
You can download the complete examples and data files from GitHub.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.