Convert HTML Template to HTML in C#

To convert an HTML template to HTML in C#, call Converter.ConvertTemplate() with the template source, TemplateData, TemplateLoadOptions, and output path. Aspose.HTML for .NET replaces template expressions such as {{expression}} with values from XML or JSON data.

Aspose.HTML for .NET offers a set of ConvertTemplate() methods that convert an HTML template into a complete HTML document. The methods can take a template file path or an HTMLDocument, a data source, load options, and the output file path.

An HTML template is HTML markup with placeholders for dynamic data. These placeholders are inline expressions enclosed in double curly braces, for example {{expression}}. During template processing, ConvertTemplate() substitutes expressions with values from the data source. The data source can be XML or JSON.

Convert Template to HTML with One API Call

Once you have prepared an HTML template and a data source, you can populate the template in a single API call. Use the ConvertTemplate(sourcePath, data, options, outputPath) overload when the template is stored as a file.

To convert a template file to HTML:

  1. Prepare an HTML template file that contains inline expressions.
  2. Create a TemplateData object from the XML or JSON data source.
  3. Create TemplateLoadOptions for template loading.
  4. Call Converter.ConvertTemplate(sourcePath, data, options, outputPath).
  5. Save the populated HTML document to the output path.
1// Convert Template to HTML using C#
2
3// Convert template to HTML
4Converter.ConvertTemplate(
5    Path.Combine(DataDir, "template.html"),
6    new TemplateData(Path.Combine(DataDir, "data-source.json")),
7    new TemplateLoadOptions(),
8    Path.Combine(OutputDir, "template-with-single-line.html")
9);

The example uses template.html as the source template, data-source.json as the data source, default TemplateLoadOptions, and saves the populated document as template-with-single-line.html.

Convert Template to HTML

Use the ConvertTemplate(HTMLDocument, TemplateData, TemplateLoadOptions, string) overload when the template is already loaded as an HTMLDocument. The method takes four parameters:

  1. HTMLDocument object used as the template source.
  2. TemplateData object that holds the data used to populate the template.
  3. TemplateLoadOptions object that provides template loading options.
  4. string value that specifies the full output HTML file path.

To convert a loaded template document:

  1. Prepare a path to the HTML template file.
  2. Prepare a path to the XML data file.
  3. Create a TemplateData object from the XML file.
  4. Create default TemplateLoadOptions.
  5. Load the template with HTMLDocument.
  6. Call Converter.ConvertTemplate(document, templateData, options, resultPath).
  7. Dispose the HTMLDocument after conversion.
 1// Populate HTML template with external XML data using C#
 2
 3// Prepare a path to an HTML source file
 4string sourcePath = Path.Combine(DataDir, "template.html");
 5
 6// Prepare a path to an xml template data file
 7string templateDataPath = Path.Combine(DataDir, "templateData.xml");
 8
 9// Define TemplateData object instance
10TemplateData templateData = new TemplateData(templateDataPath);
11
12// Prepare a path to the result file
13string resultPath = Path.Combine(OutputDir, "result.html");
14
15// Define default TemplateLoadOptions object
16TemplateLoadOptions options = new TemplateLoadOptions();
17
18// Initialize an HTML document as conversion source
19HTMLDocument document = new HTMLDocument(sourcePath, new Configuration());
20
21// Convert template to HTML
22Converter.ConvertTemplate(document, templateData, options, resultPath);
23
24// Clear resources
25document.Dispose();

The code creates an HTMLDocument from template.html and uses Converter.ConvertTemplate() to populate it with data from templateData.xml. The resulting HTML document is saved to result.html, and the source document object is disposed after conversion.

Create and Convert a Template On the Fly

You can also create both the data source and the template during runtime. This is useful when your application generates structured data and HTML markup before saving the final HTML output.

To create and convert an HTML template on the fly:

  1. Prepare JSON or XML data and save it to a file.
  2. Prepare HTML template markup with inline expressions such as {{FirstName}}.
  3. Save the template markup to a file.
  4. Create TemplateData from the data source file.
  5. Create TemplateLoadOptions.
  6. Invoke ConvertTemplate() with sourcePath, data, options, and outputPath.
 1// Populate an HTML template with structured JSON data using C#
 2
 3// Prepare a path to JSON data source file
 4string jsonPath = Path.Combine(OutputDir, "data-source.json");
 5
 6// Prepare a JSON data source and save it to the file
 7string data = @"{
 8    ""FirstName"": ""John"",
 9    ""LastName"": ""Doe"",
10    ""Address"": {
11        ""City"": ""Dallas"",
12        ""Street"": ""Austin rd."",
13        ""Number"": ""200""
14        }
15    }";
16File.WriteAllText(jsonPath, data);
17
18// Prepare a path to an HTML Template file
19string sourcePath = Path.Combine(OutputDir, "template.html");
20
21// Prepare an HTML Template and save it to the file
22string template = @"
23    <table border=1>
24        <tr>
25            <th>Person</th>
26            <th>Address</th>
27        </tr>
28        <tr>
29            <td>{{FirstName}} {{LastName}}</td>
30            <td>{{Address.Street}} {{Address.Number}}, {{Address.City}}</td>
31        </tr>
32    </table>
33    ";
34File.WriteAllText(sourcePath, template);
35
36// Prepare a path to the output file (data-filled template file)
37string outputPath = Path.Combine(OutputDir, "template-output.html");
38
39// Invoke Converter.ConvertTemplate() method in order to populate "template.html" with the data source from "data-source.json" file
40Converter.ConvertTemplate(sourcePath, new TemplateData(jsonPath), new TemplateLoadOptions(), outputPath);

The example writes data-source.json and template.html, then calls Converter.ConvertTemplate() to create template-output.html. The template uses inline expressions such as {{FirstName}}, {{LastName}}, and {{Address.City}}, and the values are taken from the JSON data source.

You can download the complete examples and data files from GitHub.

FAQ

Which data sources can populate an HTML template?

Aspose.HTML for .NET can populate HTML templates from XML or JSON data sources by using TemplateData.

Which ConvertTemplate overload should I use?

Use the file-path overload when the template is stored on disk. Use the HTMLDocument overload when the template is already loaded as a document object.

What does TemplateLoadOptions control?

TemplateLoadOptions provides options for loading and matching template data during template processing.

Related Articles