[stringtemplate-interest] Merge email using StringTemplate.NET through multiple passes

Tam Ly tamcly at gmail.com
Sat Nov 15 16:18:21 PST 2008


I am new to StringTemplate.NET and appreciate your help. I am trying to use
StringTemplate.NET (antlr.runtime.dll 2.7.7.1 & StringTemplate.dll
3.0.1.21733) to generate html emails to be sent from my web site. I am using
a database to store my content as followed:

ResourceName            ResourceValue
-----------------       ----------------------------------
EmailHeader             <center>If you have trouble viewing this email, <a
href='
https://www.chaseidprotect.com/displayEmail.aspx?mid=$MemberId$&eid=$EmailDocumentId$'>click
here</a></center><br />
                       <img src='
https://www.chaseidprotect.com/images/companylogo.jpg' alt='Chase' />
EmailWelcome            $EmailHeader$<br />
                       Dear $FirstName$ $LastName$,
                       Hello world! This is a test of StringTemplate!
There is another table that tells me that EmailWelcome resource uses
EmailHeader resource. For performance reasons, I want to cache my resource
so I have to merge data in 2 passes:
1st pass: Merge the value of EmailHeader to EmailWelcome
2nd pass: Merge my member and email template data to EmailWelcome
Here's the sample code:
      private void button2_Click(object sender, EventArgs e)
      {
          string emailHeader = "<center>If you have trouble viewing this
email, <a href='
https://www.chaseidprotect.com/displayEmail.aspx?mid=$MemberId$&eid=$EmailDocumentId$'>click
here</a></center><br /><img src='
https://www.chaseidprotect.com/images/companylogo.jpg/companylogo.jpg'
alt='Chase' />";
          string emailWelcome = "$EmailHeader$<br />Dear $FirstName$
$LastName$, <br />Hello world! This is a test of StringTemplate!";
          // 1st pass, merge resource only because we do not have member or
email template data yet
          StringTemplate emailContent = new StringTemplate(emailWelcome,
typeof(DefaultTemplateLexer));
          emailContent.SetAttribute("EmailHeader", emailHeader);
          Debug.WriteLine(emailContent.ToString());
          // 2nd pass, merge member and email template data when an email is
queued
          StringTemplate emailMerged = new
StringTemplate(emailContent.ToString(), typeof(DefaultTemplateLexer));
          emailMerged.SetAttribute("MemberId", 241);
          emailMerged.SetAttribute("EmailDocumentId", 1);
          emailMerged.SetAttribute("FirstName", "Tam");
          emailMerged.SetAttribute("LastName", "Ly");
          Debug.WriteLine(emailMerged.ToString());
      }

Because I do not have my member's and email template data: MemberId,
FirstName, LastName and EmailDocumentId at the 1st pass, I have to merge my
member and email template data at the 2nd pass.
Unfortunately, my member's attributes: $FirstName$ and $LastName no longer
exist in the EmailWelcome after my 1st pass of merging.
It looks like StringTemplate evaluates the attribute to emptry string if I
don't pass in the value the first time. Is there a way to tell
StringTemplate to pass through an attribute if a value is not
passed in instead of evaluating to empty string?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20081115/323717bb/attachment.html 


More information about the stringtemplate-interest mailing list