在VS2010中使用T4创建HTML电子邮件正文

| 我是.NET文本转换的新手,我正在尝试使用t4生成动态电子邮件正文。 因此,举个例子,我有一个例程正在构建一些字符串,如下所示。我如何将其转换为t4模板而不是StringBuilder字符串。它是运行时模板还是设计时模板?
private string BuildClosingContents(string legalBrand, string legalPhone, 
                                    string legalURL, string transText, string instructText, MyMail.Setting emailConfig)
{
    StringBuilder sbHTML = new StringBuilder();
    sbHTML.AppendLine(\"<div id=\\\"closingText\\\">\");
    if (emailConfig.Mode == \"Q\")
    {
        sbHTML.AppendLine(\"Please call me on \" + legalPhone + \" and we will arrange \");
        sbHTML.AppendLine(\"everything for you. Please have your ticket details to hand \");
        sbHTML.AppendLine(\"and your \" + instructText + \" will be required. In the meantime if you have any questions \");
        sbHTML.AppendLine(\"please do not hesitate to call me.\");
        sbHTML.AppendLine(\"<br /><br />\");
    }
    else
    {
        sbHTML.AppendLine(\"If you have any queries relating to your quote \" + transText + \" please do not hesitate \");
        sbHTML.AppendLine(\"to contact your conveyancer or me.\");
        sbHTML.AppendLine(\"<br /><br />\");
    }

    // Signature + branding
    sbHTML.AppendLine(\"Yours sincerely\");
    sbHTML.AppendLine(\"<br />\");
    sbHTML.AppendLine(\"<img src=\\\"\" + legalURL + \"/images/agent_sign.jpg\\\" width=\\\"80\\\" height=\\\"74\\\" alt=\\\"\\\"><br />\");
    //sbHTML.AppendLine(legalBrand + \" Services<br />\");
    sbHTML.AppendLine(\"</div>\");

    // Return the closing content
    return sbHTML.ToString();
}
什么是从应用程序核心逻辑之外获取这些类似助手文本例程并通过t4模板文件为电子邮件正文构建灵活的html字符串的最佳方法-谢谢     
已邀请:
        一个简单的例子是:
<#@ template language=\"C#\" #>
The first string is <#= this.FirstString #>.
<# if (!string.IsNullOrEmpty(this.SecondString)) { #>The second string is <#= this.SecondString #>.<# } #>
<#+ 

public string FirstString { get; set; }
public string SecondString { get; set; }

#>
假设文件名为MyTemplate.tt,则可以按以下方式使用它:
var template = new MyTemplate() {
    FirstString = \"Test\"
};

var text = template.TransformText();
一个很好的教程可以在这里找到。     
        为此,您将使用运行时(或经过预处理的)模板,因为这是可以结合到您的应用程序中的唯一模板。相比之下,设计时在Visual Studio中执行。     

要回复问题请先登录注册