4

I have an HTML file that will act as a template for an email that I am going to send out. There are fields in the html that are variable. I was wondering if there is a robust way to replace the placeholders in the HTML file with the variables. I know I could string.Replace all of them, but that isn't ideal since I have a lot of variables. Here is what the html file looks like

<html>
<head>
<title></title>
</head>
<body>
<div>
    Please read the Cruise Control Details Below<br>
    <br>
    <table width='100%'>
        <tr>
            <td width='100%' colspan='5'>
                <font size='4'><b>Release Details</b></font>
            </td>
        </tr>
        <tr>
            <td width='20%'>
                <b>RFC Ticket #</b>
            </td>
            <td>
                %release.RFCTicket%
            </td>
            <td>
                &nbsp;
            </td>
            <td>
                &nbsp;
            </td>
            <td width='10%'>
                &nbsp;
            </td>
            <td width='20%'>
                <b>Project / Release Name</b>
            </td>
            <td width='20%'>
                %release.ReleaseName%
            </td>
        </tr>
        <tr>
            <td width='20%'>
                <b>Release Date</b>
            </td>
            <td width='20%'>
                %release.ReleaseDateString%
            </td>
            <td>
                &nbsp;
            </td>
            <td>
                &nbsp;
            </td>
            <td width='10%'>
                &nbsp;
            </td>
            <td width='20%'>
                <b>Release Time</b>
            </td>
            <td width='20%'>
                %release.ReleaseTimeString%
            </td>
        </tr>
        <tr>
            <td width='20%'>
                <b>CAB Approval Status</b>
            </td>
            <td width='20%'>
                %release.CABApproval%
            </td>
        </tr>
        <tr>
            <td width='100%' colspan='5'>
                &nbsp;
            </td>
        </tr>
        <tr>
            <td width='100%' colspan='5'>
                <font size='4'><b>Contact Information:</b></font>
            </td>
        </tr>
        <tr>
            <td width='20%'>
                <b>Project / Team Lead</b>
            </td>
            <td width='20%'>
                %release.TeamLead%
            </td>
            <td width='10%'>
                &nbsp;
            </td>
            <td width='20%'>
                <b>On Call DSE</b>
            </td>
            <td width='20%'>
                %release.OnCallDSE%
            </td>
        </tr>
        <tr>
            <td width='20%'>
                <b>Phone</b>
            </td>
            <td width='20%'>
                %release.ContactInfo%
            </td>
            <td>
                &nbsp;
            </td>
            <td>
                &nbsp;
            </td>
            <td>
                &nbsp;
            </td>
            <td width='10%'>
                &nbsp;
            </td>
            <td width='20%'>
                <b>Phone</b>
            </td>
            <td width='20%'>
                %release.OnCallDSEContact%
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;
            </td>
        </tr>
        <tr>
            <td width='100%' colspan='5'>
                <font size='4'><b>Migration Details:</b></font>
            </td>
        </tr>
        <tr>
            <td width='20%'>
                <b>Deploy Dashboard</b>
            </td>
            <td width='20%'>
                &nbsp;
            </td>
            <td width='10%'>
                &nbsp;
            </td>
            <td width='20%'>
                <td>
                    &nbsp;
                </td>
                <td>
                    &nbsp;
                </td>
                <b>Deploy Task</b>
            </td>
            <td width='20%'>
                &nbsp;
            </td>
        </tr>
        %createTaskTable(ParseSpecialInstuctions().Split('|'))%</table>
</div>

I would like to replace the values in between the "%%" with the variable in code that represents them. I could easily

string.Replace("%release.RFCTicket%",release.RFCTicket);

But that's a bit convoluted in my opinion since I have like 10 or so variables in the file. Are there any built in methods that do what I am asking? Any help would be appreciated, thanks!

3
  • Did you already consider to serialize your content to xml and transform to the final html using xslt? This would allow to make future changes without any modifications to the processing code - just changing the xslt when needed. Commented Feb 17, 2012 at 15:01
  • 1
    do your release objects property names match exactly those in the template? Commented Feb 17, 2012 at 15:02
  • @AlexK. yes they do, that line above (the replace) is what I am doing now to do this Commented Feb 17, 2012 at 15:14

4 Answers 4

4

Use a regular expression to find your matches. I believe the appropriate regular expression would be along the lines of:

%release.\S+%

From there, you can examine each match, and parse the member name from the match. From there you can get the value of the member from your instance (release in this case) via reflection, and do a string replace.

Something like this. It could use some refactoring to eliminate redundant calls, and I don't know if it fully works, but you get the idea...

var regex = new Regex("%release.\S+%");
var match = r.Match(htmlText);
while (match.Success) 
{   
    var value = match.Value;
    var memberName = ParseMemberName(value); //Some code you write to parse out the member name from the match value
    var propertyInfo = release.GetType().GetProperty(memberName);
    var memberValue = propertyInfo.GetValue(release, null);
    htmlText = htmlText.Replace(value, memberValue != null ? memberValue.ToString() : string.Empty);
    match = match.NextMatch();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Copy paste job to do exactly what I wanted. I just replaced the parse name.. Great stuff!!!
Though I think you may need to escape the "." character. You may get some false positives, now that I think about it.
0

This is a talor made Probel for a preprocessed t4 template

You can have your help preformated in the template and allow the template engine to do the replacement. A small example below.

<div>
    Please read the Cruise Control Details Below<br>
    <br>
    <table width='100%'>
        <tr>
            <td width='100%' colspan='5'>
                <font size='4'><b>Release Details</b></font>
            </td>
        </tr>
        <tr>
            <td width='20%'>
                <b>RFC Ticket #</b>
            </td>
        <td>
            <#= RCFTicketVariable #>
        </td>

4 Comments

is there something special I need to do on the processing side, or do they get read in at runtime?
A preprocessed template get processed at runtime. Just add a constructor that takes your data then splat it out.
I am wondering how much different that is than replace. In the constructor, wouldn't you still have to loop through the file and replace occurrences?
it leaves placeholder to do the insert. and provides a lot more functionality then replace if you ever need to change the template.
0

You can use the Apache Velocity Engine port to .Net to do the templating for you

http://velocity.apache.org/engine/

http://velocity.apache.org/engine/devel/user-guide.html

http://nvelocity.sourceforge.net/

Comments

0

I would consider using REGEX (regular expressions) and giving the placeholders some sort of a special tag (ex: ) so you loop for all the tags that begin with .

Then you fill your data with a list or datatable and do 1 single loop for the whole replaces.

check these for help:

http://msdn.microsoft.com/en-us/library/twcw2f1c.aspx

http://www.regular-expressions.info/examples.html (ur exact case is mentioned under Grabbing HTML Tags)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.