C# Question

Place holder for unspecifed number of variable in html/C#

I'm making a HTML mail template. How would someone go around making a placeholder in html? i have an array of strings i want to parse in, in a certain column. But as expected from my code it only replaces the text "row1" once, so i only get the first string item.
so how do i make a string[] placeholder in html that will just add a new line for each string in the array? So what to replace "Row1" with?

The html code:

<table border="0" cellpadding="10" cellspacing="0" width="100%">
<tr>
<td class="leftColumnContent">
<p>Row1</p>
</td>
</tr>

</table>


The C# mail code ive tried:

StreamReader myreaderhtml = new StreamReader("htmlemail.html");
string[] lines = File.ReadAllLines("VUCresult.txt");
string htmlmailbody = myreaderhtml.ReadToEnd();



foreach (string s in lines)
{

htmlmailbody = htmlmailbody.Replace("Row2", namingConversion.GetADUserDisplayName(s));
}

for (int i = 0; i < lines.Length; i++)
{
htmlmailbody = htmlmailbody.Replace("Row1", lines[i]);
}

Answer

You're trying to reinvent the wheel that's called a text templating engine. Plenty of solutions exist already for this problem, and implementing it yourself is quite a bit of work. You could take a look at Razor, see Is it possible to use Razor View Engine outside asp.net.

Anyhow, to support looping, you'll need to make up some kind of indicator in your template saying "repeat this for every row".

You could do that something like this:

$repeat(myTableRow)
<tr>
    <td class="leftColumnContent">
        <p>$value1</p>
    </td>
    <td class="leftColumnContent">
        <p>$value2</p>
    </td>
</tr>
$endrepeat(myTableRow)

Then in your code, you'll have to find this block by name ("myTableRow"), and save the inner text (<tr>...</tr>).

Then you can replace the placeholders ($value1, $value2) in that inner text for every row, and output it.