Convert Template to HTML – C# Examples

Aspose.HTML for .NET offers a set of ConvertTemplate() methods that are used to convert an HTML template into an HTML document. The methods take several parameters, such as the path to the template file, the data source, and the load options, and return an HTML document. The HTML template is a piece of HTML code that contains placeholders for dynamic data. These placeholders are referred to as expressions and they are enclosed within double curly braces, like this: {{expression}}. The data source provides the actual values for these expressions. The ConvertTemplate() method substitutes the expressions in the template with values from the data source, generating a complete HTML document. This method can be used with various data source types, such as XML and JSON.

This article describes how to convert template to HTML in C# examples using Aspose.HTML for .NET library.

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;
 4...
 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    );

Convert Template to HTML

Let’s use the ConvertTemplate(HTMLDocument, TemplateData, TemplateLoadOptions, string) method to convert an HTML template into a real HTML document. The method takes four parameters:

  1. HTMLDocument object is used as a source for the template.
  2. TemplateData object holds the data that will be used to populate the template.
  3. TemplateLoadOptions object provides options for loading the template.
  4. A string parameter is used to specify the full HTML file path as output conversion result.

The method uses the information from these parameters to generate an HTML document.

 1using System.IO;
 2using Aspose.Html.IO;
 3using Aspose.Html.Saving;
 4using Aspose.Html.Converters;
 5...
 6
 7    // Prepare a path to an HTML source file
 8    var sourcePath = Path.Combine(DataDir, "template.html");
 9
10    // Prepare a path to an xml template data file
11    var templateDataPath = Path.Combine(DataDir, "templateData.xml");
12
13    // Define TemplateData object instance
14    var templateData = new TemplateData(templateDataPath);
15
16    // Prepare a path to the result file
17    var resultPath = Path.Combine(OutputDir, "result.html");
18
19    // Define default TemplateLoadOptions object
20    var options = new TemplateLoadOptions();
21
22    // Initialize an HTML document as conversion source
23    var document = new HTMLDocument(sourcePath, new Configuration());
24
25    // Convert template to HTML
26    Converter.ConvertTemplate(document, templateData, options, resultPath);
27
28    // Clear resources
29    document.Dispose();

The code creates an instance of the HTMLDocument class and uses the Converter.ConvertTemplate method() to convert a template file (template.html) to an HTML document using a data source file (templateData.xml). The method takes four parameters: the HTMLDocument object, the TemplateData object created using the data source file, the TemplateLoadOptions object, and the file path for the resulting HTML document. The resulting HTML document is saved to the specified file path (result.html). After the conversion, the document object is disposed.

Convert Template to HTML on-the-fly

If your case involves specifying data and creating a template on-the-fly, you have to follow a few steps:

  1. Prepare an HTML Template and save it to a file. The ConvertTemplate() method takes the template file’s path as a parameter (sourcePath).
  2. Prepare a JSON & 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 the ConvertTemplate()method.
  3. Initialize an instance of the TemplateLoadOptions class to determine whether the template and data item names match, regardless of case or not (options).
  4. Invoke the ConvertTemplate() method and pass sourcePath, data, options, and outputPath for it. OutputPath is a path for data-filled template file saving.
 1using System.IO;
 2using Aspose.Html.Converters;
 3using Aspose.Html.Loading;
 4...
 5
 6	// Prepare a path to an HTML Template file
 7	string sourcePath = Path.Combine(OutputDir, "template.html");
 8
 9	// Prepare an HTML Template and save it to the file
10	var template = @"
11		<table border=1>
12			<tr>
13				<th>Person</th>
14				<th>Address</th>
15			</tr>
16			<tr>
17				<td>{{FirstName}} {{LastName}}</td>
18				<td>{{Address.Street}} {{Address.Number}}, {{Address.City}}</td>
19			</tr>
20		</table>
21		";
22	File.WriteAllText(sourcePath, template);
23
24	// Prepare a path to JSON data source file
25	string jsonPath = Path.Combine(OutputDir, "data-source.json");
26
27	// Prepare a JSON data source and save it to the file
28	var data = @"{
29		'FirstName': 'John',
30		'LastName': 'Doe',
31		'Address': {
32			'City': 'Dallas',
33			'Street': 'Austin rd.',
34			'Number': '200'
35			}
36		}";
37	File.WriteAllText(jsonPath, data);
38
39	// Prepare a path to the output file (data-filled template file)
40	string outputPath = Path.Combine(OutputDir, "template-output.html");
41
42	// Invoke Converter.ConvertTemplate in order to populate 'template.html' with the data source from 'data-source.json' file
43	Converter.ConvertTemplate(sourcePath, new TemplateData(jsonPath), new TemplateLoadOptions(), outputPath);

Aspose.HTML offers free online Converters for converting HTML, XHTML, MHTML, EPUB, XML and Markdown files to a variety of popular formats. You can easily convert HTML to PDF, HTML to JPG, SVG to PDF, MHTML to PDF, or MD to HTML. Just select a file, choose the format to convert, and you’re done. It’s fast and completely free!

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.