从.NET应用程序生成使用模板HTML应用程序、模板、NET、HTML

2023-09-03 07:47:21 作者:假正经^O^

我有需要产生一些HTML文件的.NET控制台应用程序。我可以建立在一个StringBuilder的HTML写的内容到一个文件,但我想这将是更好的通过它使用某种模板文件与占位符,其中数据去,然后再处理我的数据在运行时。

我猜有很多方法可以使用​​ASPX,或T4,或一些可以在ASP.NET MVC使用另一种观点引擎,但我​​不知道什么是最容易集成到一个控制台应用程序(或如何我会去整合它们)。

我希望最终能够调用一些形式:

  GenerateHtml(htmlPath,模板,customDataObject);
 

解决方案

马特提到,火花是好的,但它可能是矫枉过正的一些简单的模板,得到,如果你不使用它的MVC复杂。

我个人有很多成功的与NVelocity,我也用它发布一个简单的例子/包装:的 http://simpable.com/$c$c/simpletemplate/

此外,在NVelocity GraffitiCMS的整个主题化的系统运行(虽然我会用火花,如果它是可用于此任务)。

- 斯科特

I have a .NET console application that needs to generate some HTML files. I could just construct the HTML in a StringBuilder and write the contents out to a file, but I was thinking it would be nicer to use some kind of template file with placeholders for where the data goes and then process my data through it at runtime.

.NET Core 编程指南中文版 2.3 入门 在 Visual Studio 2017 中使用 .NET Core SDK 生成 C Hello World 应用程序

I'm guessing there are ways to use aspx, or T4, or some of the alternative view engines that you can use with ASP.NET MVC, but I don't know what would be easiest to integrate into a console application (or how I would go about integrating them).

I want to end up able to call something of the form:

 GenerateHtml(htmlPath, template, customDataObject);

解决方案

As Matt mentioned, spark is nice but it could be overkill for some simple templates and gets complicated if you are not using it in MVC.

I have personally had a lot of success with NVelocity and I also publish a simple example/wrapper on using it: http://simpable.com/code/simpletemplate/

In addition, GraffitiCMS's entire theming system run on NVelocity (although I would have used spark if it were available for this task).

-Scott