Generating HTML using a template from a .NET application

前端 未结 6 584
后悔当初
后悔当初 2021-01-04 20:35

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

相关标签:
6条回答
  • 2021-01-04 21:25

    One way you could do this is create a XSL file as the template, serialise your customDataObject as XML then perform a transform to generate the required HTML.

    Update: Whilst I like (and do use) the string replacement method advocated by other folks here, there is a certain flexibility to using XML/XSL. Say your object has a property that is a list, for example an order object with a list of line item objects, you pretty much have to burn into your code the logic that has to render the line items.

    With XSL all you do is pass the serialised order object XML to the XSL and let the XSL handle whatever HTML it needs to generate. This means you can often edit the XSL in place or have variants (order summary, detailed order etc) without adding extra code to your app with all the extra hassle of rebuild/deploy.

    But then it all depends on the complexity of what you need to render, for some jobs string replacement is more obvious, for others XSL is the way. As I said, we use both.

    0 讨论(0)
  • 2021-01-04 21:28

    Here's some code that illustrates a fairly simple way to accomplish what you're trying to do:

    using System;
    using System.IO;
    
    public class HtmlTemplate
    {
        private string _html;
    
        public HtmlTemplate(string templatePath)
        {
            using (var reader = new StreamReader(templatePath))
                _html = reader.ReadToEnd();
        }
    
        public string Render(object values)
        {
            string output = _html;
            foreach (var p in values.GetType().GetProperties())
                output = output.Replace("[" + p.Name + "]", (p.GetValue(values, null) as string) ?? string.Empty);
            return output;
        }
    }
    
    public class Program
    {
        void Main()
        {
            var template = new HtmlTemplate(@"C:\MyTemplate.txt");
            var output = template.Render(new {
                TITLE = "My Web Page",
                METAKEYWORDS = "Keyword1, Keyword2, Keyword3",
                BODY = "Body content goes here",
                ETC = "etc"
            });
            Console.WriteLine(output);
        }
    }
    

    Using this, all you have to do is create some HTML templates and fill them with replaceable tokens such as [TITLE], [METAKEYWORDS], etc. Then pass in anonymous objects that contain the values to replace the tokens with. You could also replace the value object with a dictionary or something similar.

    0 讨论(0)
  • 2021-01-04 21:28

    Check out docu. It uses the Spark view engine to render templated HTML from a console app. Pretty straightforward.

    0 讨论(0)
  • 2021-01-04 21:29

    Another option instead of using XSLT as Kev suggests is to use named string formatting. Using code like this example by Phil Haack.

    Here you can have your template as a string (read from a file maybe) and format it using the given object.

    Now you could do something like this:

    var person = new { FirstName = "rune", LastName = "grimstad" };
    string template = "<html><body><h1>Hello {FirstName} {LastName}</h1></body></html>";
    string html = NamedFormat(template, person);
    
    0 讨论(0)
  • 2021-01-04 21:38

    There are several ways,

    1- Using view engine (Razor, Spark, ...) - this is good if you are using mvc

    2- Using XSLT file and transform the given xml to Html

    3- Using T4 Text Templates (Best performance, easy to understand)

    0 讨论(0)
  • 2021-01-04 21:39

    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

    0 讨论(0)
提交回复
热议问题