Skip to content

Instantly share code, notes, and snippets.

@hydr
Created March 20, 2013 12:19
Show Gist options
  • Save hydr/5204242 to your computer and use it in GitHub Desktop.
Save hydr/5204242 to your computer and use it in GitHub Desktop.
Custom BaseTemplate for ActionMailer.Standalone
public class RazorTemplateBase<T> : TemplateBase<T>
{
private StandaloneHtmlHelpers _html;
public StandaloneHtmlHelpers Html
{
get
{
_html = _html ?? new StandaloneHtmlHelpers(TemplateService, ViewBag);
return _html;
}
}
}
public class StandaloneHtmlHelpers
{
public StandaloneHtmlHelpers()
{
_executeContext = new ExecuteContext();
_templateService = new TemplateService();
}
public StandaloneHtmlHelpers(ITemplateService templateService = null, DynamicViewBag viewBag = null)
{
_executeContext = (viewBag == null) ? new ExecuteContext() : new ExecuteContext(viewBag);
_templateService = templateService ?? new TemplateService();
}
private readonly ExecuteContext _executeContext;
private readonly ITemplateService _templateService;
public IEncodedString Raw(string input)
{
return new RawString(input);
}
public TemplateWriter Partial(string templateName, object model = null)
{
var template = _templateService.Resolve(templateName, model);
if(template == null)
throw new ArgumentException("No template could be resolved with the name '" + templateName + "'");
return new TemplateWriter(tw => tw.Write(template.Run(_executeContext)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment