/// <summary> /// Run the template /// </summary> /// <typeparam name="TModel"></typeparam> /// <param name="templateKey"></param> /// <param name="template"></param> /// <param name="model"></param> /// <returns></returns> private void Merge <TModel>(string templateKey, Model.IEmailTemplate template, TModel model) { var type = typeof(TModel) == typeof(object) || typeof(TModel).IsAnonymousType() ? null : typeof(TModel); template.Subject = Engine.Razor.Run(String.Format(SUBJECT_TEMPLATE_KEY, templateKey), type, model); template.Body = Engine.Razor.Run(String.Format(BODY_TEMPLATE_KEY, templateKey), type, model); }
/// <summary> /// Build the specified 'template' and merge the specified 'model'. /// Mutate the specified 'template' Subject and Body properties. /// </summary> /// <typeparam name="TModel"></typeparam> /// <param name="templateKey"></param> /// <param name="template"></param> /// <param name="model"></param> public void Build <TModel>(string templateKey, Model.IEmailTemplate template, TModel model) { if (String.IsNullOrWhiteSpace(templateKey)) { throw new ArgumentException("Argument is required and cannot be null, empty or whitespace.", nameof(templateKey)); } if (template == null) { throw new ArgumentNullException(nameof(template)); } CompileTemplate <TModel>(templateKey, template); Merge(templateKey, template, model); }
/// <summary> /// Compile the templates before running them. /// </summary> /// <param name="options"></param> private void CompileTemplate <TModel>(string templateKey, Model.IEmailTemplate template) { var subjectKey = String.Format(SUBJECT_TEMPLATE_KEY, templateKey); var type = typeof(TModel) == typeof(object) || typeof(TModel).IsAnonymousType() ? null : typeof(TModel); if (!_cache.ContainsKey(subjectKey)) { Engine.Razor.Compile(template.Subject, subjectKey, type); _cache.Add(subjectKey, template); } var bodyKey = String.Format(BODY_TEMPLATE_KEY, templateKey); if (!_cache.ContainsKey(bodyKey)) { Engine.Razor.Compile(template.Body, bodyKey, type); _cache.Add(bodyKey, template); } }