Created
March 20, 2013 12:19
-
-
Save hydr/5204242 to your computer and use it in GitHub Desktop.
Custom BaseTemplate for ActionMailer.Standalone
This file contains 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
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