HTML Template – Convert Template to HTML
This article describes how to use Aspose.HTML for Java to create an HTML document based on a template and populate it with data from various sources, such as XML or JSON. Using inline expressions syntax, you can efficiently match data-sources against your HTML template and dynamically generate content.
Aspose.HTML for Java offers a set of convertTemplate() methods that are used to convert an HTML template into an HTML document. The methods take several parameters (for example, paths to the template and data source, an object of TemplateLoadOptions, and a path to the result HTML) and return the populated HTML document.
Understanding Template Markup
An HTML template is a standard HTML file that contains some special inline expressions. These expressions use double curly braces {{ }} to map data from the input data source to specific parts of the HTML document. When processed, these template markers are replaced with corresponding data values following the below-mentioned rules.
Inline Expressions Syntax
Following is the list of supported inline expressions syntax.
{{ … }} – data-binding expression
The data-binding expression is used to set values of 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
1<Data>
2<FirstName>John</FirstName>
3<LastName>Doe</LastName>
4<Address>
5 <City>Chicago</City>
6 <Street>Oakmound Drive</Street>
7 <Number>100</Number>
8</Address>
9</Data>
The data-binding expression in the HTML page template:
HTML Template
This HTML template demonstrates the use of inline expressions for data binding. It features a table with two columns: “Person” and “Address.” The “Person” column dynamically displays data for {{FirstName}} and {{LastName}} from the data source, while the “Address” column shows {{Address.Street}}, {{Address.Number}}, and {{Address.City}}. Inline expressions are defined using curly-brace syntax.
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>
10</table>
{{#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 the 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>100</Number>
8 <Street>Oakmound Drive</Street>
9 <City>Chicago</City>
10 </Address>
11 <Phone1>773-349-7747</Phone1>
12 <Phone2>708-252-9790</Phone2>
13 </Person>
14 <Person>
15 <FirstName>Jack</FirstName>
16 <LastName>Fox</LastName>
17 <Address>
18 <Number>355</Number>
19 <Street>Haul Road</Street>
20 <City>San Francisco</City>
21 </Address>
22 <Phone1>650-942-7641</Phone1>
23 </Person>
24 <Person>
25 <FirstName>Sherlock</FirstName>
26 <LastName>Holmes</LastName>
27 <Address>
28 <Number>45</Number>
29 <Street>Baker str.</Street>
30 <City>London</City>
31 </Address>
32 <Phone1>012-5146-564</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 supports foreach directive expression only for the following list of 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-source:
XML Data Source
1 <Data>
2 <FirstName>John</FirstName>
3 <LastName>Doe</LastName>
4 <Address>
5 <City>Chicago</City>
6 <Street>Oakmound Drive</Street>
7 <Number>100</Number>
8 </Address>
9</Data>
JSON Data Source
1{
2 "FirstName": "John",
3 "LastName": "Doe",
4 "Address": {
5 "City": "Chicago",
6 "Street": "Oakmound Drive",
7 "Number": "100"
8 }
9 }
Convert Template to HTML
If your case involves creating a template and specifying data on-the-fly, you have to follow a few steps:
- Prepare a JSON or XML data source and save it to a file. The
TemplateData() constructor takes the path to this data file and creates a data object (
data
) for theconvertTemplate(sourcePath, data, options, outputPath)
method. - Prepare an HTML template and save it to a file. The convertTemplate() method will take the template file’s path as a parameter (
sourcePath
). - Initialize an instance of the
TemplateLoadOptions class to determine whether the template and data item names match, regardless of case or not (
options
). - Invoke the
convertTemplate(
sourcePath
,data
,options
,outputPath
) method and passsourcePath
,data
,options
, andoutputPath
for it.outputPath
is a path for data-filled template file saving. The convertTemplate() method substitutes the expressions in the template with values from the data source, generating a populated HTML document.
1// Prepare a JSON data-source and save it to the file.
2String data =
3 " {\n" +
4 " 'FirstName': 'John',\n" +
5 " 'LastName': 'Smith',\n" +
6 " 'Address': {\n" +
7 " 'City': 'Dallas',\n" +
8 " 'Street': 'Austin rd.',\n" +
9 " 'Number': '200'\n" +
10 " }\n" +
11 " }\n";
12try (java.io.FileWriter fileWriter = new java.io.FileWriter("data-source.json")) {
13 fileWriter.write(data);
14}
15
16// Prepare an HTML Template and save it to the file.
17String template =
18 "<table border=1>\n" +
19 " <tr>\n" +
20 " <th>Person</th>\n" +
21 " <th>Address</th>\n" +
22 " </tr>\n" +
23 " <tr>\n" +
24 " <td>{{FirstName}} {{LastName}}</td>\n" +
25 " <td>{{Address.Street}} {{Address.Number}}, {{Address.City}}</td>\n" +
26 " </tr>\n" +
27 "</table>\n";
28try (java.io.FileWriter fileWriter = new java.io.FileWriter("template.html")) {
29 fileWriter.write(template);
30}
31
32// Invoke Converter.ConvertTemplate in order to populate 'template.html' with the data-source from 'data-source.json' file
33com.aspose.html.converters.Converter.convertTemplate(
34 "template.html",
35 new com.aspose.html.converters.TemplateData("data-source.json"),
36 new com.aspose.html.loading.TemplateLoadOptions(), "document.html"
37);
Technical Recommendations
Always validate the data source format (XML/JSON) and ensure consistency with template markers to avoid mismatches.
Use TemplateLoadOptions.NamesAreCaseSensitive to control case sensitivity during data binding. If the value is
true
, matching is case sensitive; if it isfalse
, the character case will be ignored. The default value istrue
. Setting it tofalse
simplifies handling inconsistent naming.