Created
May 14, 2012 23:34
-
-
Save teradyne/2698082 to your computer and use it in GitHub Desktop.
RazorEngine Example Template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//using the RazorEngine @http://razorengine.codeplex.com/ | |
// I am using this with ASP.NET forms (.NET 4.0) for email templates | |
// 1. Email HTML Templates Location: | |
// ~/templates/email/MyEmailTemplate.cshtml (name something useful like Page, Master where email is being sent) | |
//Helper methods - put this shit in a common helper class like below: | |
using RazorEngine; | |
using RazorEngine.Configuration; | |
using RazorEngine.Templating; | |
using System.Web; | |
//Template Helper Class | |
public class TemplateHelper | |
{ | |
//get the raw template content from file system | |
public static string GetTemplateContent(string filePath) | |
{ | |
string fileContent = string.Empty; | |
if (File.Exists(filePath)) | |
{ | |
using (StreamReader reader = new StreamReader(filePath)) | |
{ | |
fileContent = reader.ReadToEnd(); | |
} | |
} | |
return fileContent; | |
} | |
//get the parsed template content | |
public static string GetParsedEmailBodyForModel(ModelEntity model) | |
{ | |
string template = TemplateHelper.GetTemplateContent(HttpContext.Current.Server.MapPath(path_to_the_Models_Template)); | |
string bodyText = string.Empty; | |
var config = new TemplateServiceConfiguration | |
{ | |
BaseTemplateType = typeof(MyCustomEmailTemplateBase<>) | |
}; | |
using (var service = new TemplateService(config)) | |
{ | |
Razor.SetTemplateService(service); | |
bodyText = Razor.Parse(template, project); | |
} | |
return bodyText; | |
} | |
public class MyCustomEmailTemplateBase<T> : TemplateBase<T> | |
{ | |
//this method is just a sample to use within the Razor template - takes some int Id and returns entity(ORM) | |
public string MyMethodToUseWithinTheTemplate(int objectid) | |
{ | |
return new ObjectEntity(objectid).Name; | |
} | |
} | |
} | |
//MyEmailTemplate.cshtml | |
<!--- template start --> | |
<table cellpadding="2" cellspacing="0"> | |
<tr><td width="200">Project Name:</td><td>@Model.Projectname</td></tr> | |
<tr> | |
<td width="200">Created Date:</td> | |
<td>@Model.CreatedDate.Value.ToString("MM/dd/yyyy")</td> | |
</tr> | |
<tr><td colspan="2"> | |
<b>Project Lines:</b><hr/> | |
@{int projectnumber = 1;} | |
@foreach(var projectline in @Model.ProjectLineItems){ | |
<text> | |
<b>@projectnumber. Project Category : @GetProjectCategoryname(Convert.ToInt32(projectline.ProjectCategory)) </b><br/> | |
Category: @projectline.Category<br/> | |
Quantity: @projectline.Quantity<br/> | |
</text> | |
projectnumber = projectnumber + 1; | |
} | |
<hr/> | |
</td></tr> | |
<tr> | |
<td colspan="2">Project Notes:<br /> | |
@Model.Notes | |
</td> | |
</tr> | |
<tr> | |
<td width="200">Submitted at:</td> | |
<td valign="top">@Model.Projectsubmitteddate.Value</td> | |
</tr> | |
</table> | |
<!--- / template end ---> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment