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.
1// Convert template to HTML
2Converter.ConvertTemplate(
3 Path.Combine(DataDir, "template.html"),
4 new TemplateData(Path.Combine(DataDir, "data-source.json")),
5 new TemplateLoadOptions(),
6 Path.Combine(OutputDir, "template-with-single-line.html")
7);
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:
HTMLDocument
object is used as a source for the template.TemplateData
object holds the data that will be used to populate the template.TemplateLoadOptions
object provides options for loading the template.- 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.
1// Prepare a path to an HTML source file
2var sourcePath = Path.Combine(DataDir, "template.html");
3
4// Prepare a path to an xml template data file
5var templateDataPath = Path.Combine(DataDir, "templateData.xml");
6
7// Define TemplateData object instance
8var templateData = new TemplateData(templateDataPath);
9
10// Prepare a path to the result file
11var resultPath = Path.Combine(OutputDir, "result.html");
12
13// Define default TemplateLoadOptions object
14var options = new TemplateLoadOptions();
15
16// Initialize an HTML document as conversion source
17var document = new HTMLDocument(sourcePath, new Configuration());
18
19// Convert template to HTML
20Converter.ConvertTemplate(document, templateData, options, resultPath);
21
22// Clear resources
23document.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:
- Prepare an HTML Template and save it to a file. The ConvertTemplate() method takes the template file’s path as a parameter (
sourcePath
). - 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. - 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() method and pass
sourcePath
,data
,options
, andoutputPath
for it. OutputPath is a path for data-filled template file saving.
1// Prepare a path to JSON data source file
2string jsonPath = Path.Combine(OutputDir, "data-source.json");
3
4// Prepare a JSON data source and save it to the file
5var data = @"{
6 ""FirstName"": ""John"",
7 ""LastName"": ""Doe"",
8 ""Address"": {
9 ""City"": ""Dallas"",
10 ""Street"": ""Austin rd."",
11 ""Number"": ""200""
12 }
13 }";
14File.WriteAllText(jsonPath, data);
15
16// Prepare a path to an HTML Template file
17string sourcePath = Path.Combine(OutputDir, "template.html");
18
19// Prepare an HTML Template and save it to the file
20var template = @"
21 <table border=1>
22 <tr>
23 <th>Person</th>
24 <th>Address</th>
25 </tr>
26 <tr>
27 <td>{{FirstName}} {{LastName}}</td>
28 <td>{{Address.Street}} {{Address.Number}}, {{Address.City}}</td>
29 </tr>
30 </table>
31 ";
32File.WriteAllText(sourcePath, template);
33
34// Prepare a path to the output file (data-filled template file)
35string outputPath = Path.Combine(OutputDir, "template-output.html");
36
37// Invoke Converter.ConvertTemplate in order to populate 'template.html' with the data source from 'data-source.json' file
38Converter.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.