Insert data into html template

Solarion

Honorary Master
Joined
Nov 14, 2012
Messages
28,051
Reaction score
17,804
I have a basic template with several column headings. What I'm trying to do is insert rows into this template. I've been struggling for a while and even ended up in the darkest parts of Microsoft's website :giggle:

Run-Time Text Generation with T4 Text Templates

I even ended up back in 2002 doing this and my blood pressure rapidly rising in the process.

sw.WriteLine("<!DOCTYPE HTML PUBLIC " -//W3C//DTD HTML 4.0 Transitional//EN">");
sw.WriteLine("<html>");
sw.WriteLine("<head>");

Anyway that wasn't working either. It seems like such a simple task and yet finding any answers is proving to be difficult. Please give me some pointers!
 
There is a library called Aspose which may have what I need.

JSON:
var data = @"{
    'FirstName': 'John',
    'LastName': 'Smith',
    'Address': {
        'City': 'Dallas',
        'Street': 'Austin rd.',
        'Number': '200'
        }
    }";

System.IO.File.WriteAllText("data-source.json", data);

HTML:
 <table border=1 data_merge='{{#foreach Persons.Person}}'>
    <tr>
        <th>Person</th>
        <th>Address</th>
    </tr>
    <tr>
        <td>{{FirstName}} {{LastName}}</td>
        <td>{{Address.Street}} {{Address.Number}}, {{Address.City}}</td>
    </tr>
</table>
System.IO.File.WriteAllText("template.html", template);

 
I am quiet fond of Jinja2 but suppose it depends on your use case.

I'm starting to get some positive data inserted into the template and saved onto the desktop in C# using Aspose.

Just gonna work on it for a few hours. Will post results later.
 
sw.WriteLine("<!DOCTYPE HTML PUBLIC " -//W3C//DTD HTML 4.0 Transitional//EN">");
Escape your special characters inside quotes!

This should look like the below:
sw.WriteLine("<!DOCTYPE HTML PUBLIC \" -//W3C//DTD HTML 4.0 Transitional//EN\">");
 
We need some more info about the problem. Will the template change during runtime?

ASP.NET, Razor and XSLT are all technically templates.

 
The html template itself doesn't get modified since it is read into memory, merged with XML/JSON etc and then saved as a new document. I will post a full code dump of how I came right with it.

In a nutshell you have your HTML template.

HTML:
<table border=1 data_merge='{{#foreach Persons.Person}}'>
    <tr>
        <th>Person</th>
        <th>Address</th>
    </tr>
    <tr>
        <td>{{FirstName}} {{LastName}}</td>
        <td>{{Address.Street}} {{Address.Number}}, {{Address.City}}</td>
    </tr>
</table>

An XML or JSON data source could be a stream or a file or whatever your fancy is. I'm just using an XML document for now to get going.

XML:
 <Data>
      <Persons>
            <Person>
                  <FirstName>John</FirstName>
                  <LastName>Smith</LastName>
                  <Address>
                         <Number>200</Number>
                         <Street>Austin rd.</Street>
                         <City>Dallas</City>
                   </Address>
                   <Phone1>345-345-34-55</Phone1>
                   <Phone2>345-555-09-09</Phone2>
            </Person>
            <Person>
                   <FirstName>Jack</FirstName>
                   <LastName>Fox</LastName>
                   <Address>
                         <Number>25</Number>
                         <Street>Broadway</Street>
                         <City>New York</City>
                   </Address>
                   <Phone1>081-544-12-15</Phone1>
             </Person>
             <Person>
                   <FirstName>Sherlock</FirstName>
                   <LastName>Holmes</LastName>
                   <Address>
                         <Number>65</Number>
                         <Street>Baker str.</Street>
                         <City>London</City>
                    </Address>
                    <Phone1>012-5344-334</Phone1>
            </Person>
      </Persons>
</Data>

And then from code I'm calling this, console App for testing.

C#:
static void Main(string[] args)
{
    Aspose.Html.Converters.Converter.ConvertTemplate("template.html", new      Aspose.Html.Converters.TemplateData("datasource.xml"), new Aspose.Html.Loading.TemplateLoadOptions(), "document.html");
}
 
Last edited:
The html template itself doesn't get modified since it is read into memory, merged with XML/JSON etc and then saved as a new document. I will post a full code dump of how I came right with it.
I think @Barbarian Conan was speaking in context of ASP.NET, since you’re working with HTML it’s basically what it was designed to do.

The View Engine takes the dynamic template and compiles it and caches it at runtime, this makes rendering really fast because basically it’s just really huge string objects. If the View Template changes, it gets compiled on the fly.
The nice thing with Razor is you have the C# syntax available so you can do LINQ in your template if you wish.

Again it was designed to work with HTML so you don’t need to worry about escaping characters etc. That’s handled for you.
Also things like Partial Views are super handy, so you can for example update the Footer Partial and it will be applied to all your Email Templates.

The reason you probably didn’t find much resources was because in the full framework, the engine was coupled to ASP.NET, this has changed in .NET Core I believe. You can also compile your Views at build time to avoid null references which with model binding is awesome.

I did a PoC for email templating a really long time ago.
 
That aspose does look quite handy though, as you don’t need to define and marshal POCOs
 
That aspose does look quite handy though, as you don’t need to define and marshal POCOs

Yup I've come right with it. Aspose is awesome! Will dump so code in a few hours just busy doing testing generating lots of reports.
 
Top
Sign up to the MyBroadband newsletter
X