Search code examples
c#.nethtmltemplate-engine

Generating HTML using a template from a .NET application


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.

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

Solution

  • 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