I am using the Razor engine https://github.com/Antaris/RazorEngine to parse the body of my email templates. Is it possible to define a layout and include other .cshtml files
The easiest way to implement a layout with RazorEngine is by replacing what your template return in the @RenderBody() of the layout:
var finalHtml = layout.Replace(@"@RenderBody()", templateHtml);
E.g.:
Your _Layout.cshtml with the typical @RenderBody()
@RenderBody()
Your RazorEngine template MyTemplate.cshtml
@using RazorEngine.Templating
@inherits TemplateBase
Hello People
@Model
And wherever you call the RazorEngine template:
var TemplateFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "EmailTemplates");
var template = File.ReadAllText(Path.Combine(TemplateFolderPath,"MyTemplate.cshtml"));
var layout = File.ReadAllText(Path.Combine(TemplateFolderPath, "_Layout.cshtml"));
var templateService = new TemplateService();
var templateHtml = templateService.Parse(template, myModel, null, null);
var finalHtml = layout.Replace(@"@RenderBody()", templateHtml);