将模板转换为 HTML – C# 示例
Aspose.HTML for .NET 提供了一组 ConvertTemplate() 方法,用于将 HTML 模板转换为 HTML 文档。这些方法需要几个参数,如模板文件的路径、数据源和加载选项,然后返回一个 HTML 文档。HTML 模板是一段包含动态数据占位符的 HTML 代码。这些占位符被称为表达式,并用双引号括起来,如下所示:{{表达式}}。数据源为这些表达式提供实际值。ConvertTemplate() 方法将模板中的表达式替换为数据源中的值,生成一个完整的 HTML 文档。该方法可用于各种数据源类型,如 XML 和 JSON。
本文介绍如何在 C# 示例中使用 Aspose.HTML for .NET 库将模板转换为 HTML。
只需一行代码即可将模板转换为 HTML
准备好 HTML 模板后,只需一行代码就能在 C# 应用程序中将模板转换为 HTML!为此,您需要向
ConvertTemplate(sourcePath
, data
, options
, outputPath
) 方法传递所需的参数。
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);
将模板转换为 HTML
让我们使用
ConvertTemplate(HTMLDocument
, TemplateData
, TemplateLoadOptions
, string
) 方法将 HTML 模板转换为真正的 HTML 文档。该方法需要四个参数:
HTMLDocument
对象被用作模板的源代码。TemplateData
对象保存用于填充模板的数据。TemplateLoadOptions
对象提供了加载模板的选项。- 使用
string
参数指定完整的 HTML 文件路径作为输出转换结果。
该方法使用这些参数中的信息生成 HTML 文档。
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();
这段代码创建了一个
HTMLDocument 类实例,并使用
Converter.ConvertTemplate method() 将模板文件 (template.html) 转换为使用数据源文件 (templateData.xml) 的 HTML 文档。该方法需要四个参数:HTMLDocument 对象、使用数据源文件创建的 TemplateData 对象、TemplateLoadOptions 对象和生成的 HTML 文档的文件路径。生成的 HTML 文档将保存到指定的文件路径 (result.html)。转换完成后,将处置 document
对象。
即时将模板转换为 HTML
如果您的情况涉及指定数据和即时创建模板,您必须遵循几个步骤:
- 准备 HTML 模板并将其保存到文件中。ConvertTemplate() 方法将模板文件的路径作为参数(
sourcePath
)。 - 准备 JSON 和 XML 数据源并将其保存到文件中。TemplateData() 构造函数获取该数据文件的路径,并为 ConvertTemplate() 方法创建一个数据对象(
data
)。 - 初始化
TemplateLoadOptions 类的实例,以确定模板名称和数据项名称是否匹配,无论大小写(
options
)。 - 调用
ConvertTemplate() 方法并为其传递
sourcePath
、data
、options
和outputPath
。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);
Aspose.HTML 提供免费的在线 转换器,用于将 HTML、XHTML、MHTML、EPUB、XML 和 Markdown 文件转换为各种流行格式。您可以轻松地将 HTML 转换为 PDF、 HTML 转换为 JPG、 SVG 转换为 PDF、 MHTML 转换为 PDF 或 MD 转换为 HTML,只需选择文件,选择要转换的格式,就大功告成了。它速度快,而且完全免费!