public override string ParseTemplate(MessageTemplatePart template, ParseTemplateContext context) { var layout = template.Layout; var templateContent = template.Text; var viewBag = context.ViewBag; if (layout != null) { _razorMachine.RegisterLayout("~/shared/_layout.cshtml", layout.Text); templateContent = "@{ Layout = \"_layout\"; }\r\n" + templateContent; } try { // Convert viewBag to string/object pairs if required if (viewBag != null) { if (viewBag is IEnumerable <KeyValuePair <string, string> > ) { viewBag = ((IEnumerable <KeyValuePair <string, string> >)viewBag).Select(x => new KeyValuePair <string, object>(x.Key, x.Value)).ToDictionary(x => x.Key, x => x.Value); } } var tmpl = _razorMachine.ExecuteContent(templateContent, context.Model, viewBag); return(tmpl.Result); } catch (TemplateCompileException ex) { Logger.Log(LogLevel.Error, ex, "Failed to parse the {0} Razor template with layout {1}", template.Title, layout != null ? layout.Title : "[none]"); return(BuildErrorContent(ex, template, layout)); } }
public IParserEngine SelectParser(MessageTemplatePart template) { var parserId = template.DefaultParserId; IParserEngine parser = null; if (!string.IsNullOrWhiteSpace(parserId)) { parser = GetParser(parserId); } if (parser == null) { parserId = _services.WorkContext.CurrentSite.As<MessagingSiteSettingsPart>().DefaultParserId; parser = GetParser(parserId); } return parser ?? _parsers.First(); }
public override string ParseTemplate(MessageTemplatePart template, ParseTemplateContext context) { var layout = template.Layout; var templateContent = new StringBuilder(template.Text); var viewBag = context.ViewBag; if (layout != null) { templateContent = new StringBuilder(layout.Text.Replace(LayoutBeacon, templateContent.ToString())); } if (viewBag != null) { var variables = viewBag as IEnumerable<KeyValuePair<string, string>>; if (variables != null) { templateContent = variables.Aggregate(templateContent, (current, variable) => current.Replace(string.Format("[{0}]", variable.Key), variable.Value)); } } return templateContent.ToString(); }
public IParserEngine SelectParser(MessageTemplatePart template) { var parserId = template.DefaultParserId; IParserEngine parser = null; if (!string.IsNullOrWhiteSpace(parserId)) { parser = GetParser(parserId); } if (parser == null) { parserId = _services.WorkContext.CurrentSite.As <MessagingSiteSettingsPart>().DefaultParserId; parser = GetParser(parserId); } return(parser ?? _parsers.First()); }
private static string BuildErrorContent(Exception ex, MessageTemplatePart templatePart, MessageTemplatePart layout) { var sb = new StringBuilder(); var currentException = ex; while (currentException != null) { sb.AppendLine(currentException.Message); currentException = currentException.InnerException; } sb.AppendFormat("\r\nTemplate ({0}):\r\n", templatePart.Title); sb.AppendLine(templatePart.Text); if (layout != null) { sb.AppendFormat("\r\nLayout ({0}):\r\n", layout.Title); sb.AppendLine(layout.Text); } return sb.ToString(); }
private static string BuildErrorContent(Exception ex, MessageTemplatePart templatePart, MessageTemplatePart layout) { var sb = new StringBuilder(); var currentException = ex; while (currentException != null) { sb.AppendLine(currentException.Message); currentException = currentException.InnerException; } sb.AppendFormat("\r\nTemplate ({0}):\r\n", templatePart.Title); sb.AppendLine(templatePart.Text); if (layout != null) { sb.AppendFormat("\r\nLayout ({0}):\r\n", layout.Title); sb.AppendLine(layout.Text); } return(sb.ToString()); }
public override string ParseTemplate(MessageTemplatePart template, ParseTemplateContext context) { var layout = template.Layout; var templateContent = new StringBuilder(template.Text); var viewBag = context.ViewBag; if (layout != null) { templateContent = new StringBuilder(layout.Text.Replace(LayoutBeacon, templateContent.ToString())); } if (viewBag != null) { var variables = viewBag as IEnumerable <KeyValuePair <string, string> >; if (variables != null) { templateContent = variables.Aggregate(templateContent, (current, variable) => current.Replace(string.Format("[{0}]", variable.Key), variable.Value)); } } return(templateContent.ToString()); }
public override string ParseTemplate(MessageTemplatePart template, ParseTemplateContext context) { var layout = template.Layout; var templateContent = template.Text; var viewBag = context.ViewBag; if (layout != null) { _razorMachine.RegisterLayout("~/shared/_layout.cshtml", layout.Text); templateContent = "@{ Layout = \"_layout\"; }\r\n" + templateContent; } try { // Convert viewBag to string/object pairs if required if (viewBag != null) { if (viewBag is IEnumerable<KeyValuePair<string, string>>) viewBag = ((IEnumerable<KeyValuePair<string, string>>) viewBag).Select(x => new KeyValuePair<string, object>(x.Key, x.Value)).ToDictionary(x => x.Key, x => x.Value); } var tmpl = _razorMachine.ExecuteContent(templateContent, context.Model, viewBag); return tmpl.Result; } catch (TemplateCompileException ex) { Logger.Log(LogLevel.Error, ex, "Failed to parse the {0} Razor template with layout {1}", template.Title, layout != null ? layout.Title : "[none]"); return BuildErrorContent(ex, template, layout); } }
private void PropertyHandlers(ActivatedContentContext context, MessageTemplatePart part) { part.LayoutField.Loader(x => part.Record.LayoutId != null ? _messageTemplateService.GetTemplate(part.Record.LayoutId.Value) : null); part.LayoutField.Setter(x => { part.Record.LayoutId = x != null ? x.Id : default(int?); return(x); }); }
private void PropertyHandlers(ActivatedContentContext context, MessageTemplatePart part) { part.LayoutField.Loader(x => part.Record.LayoutId != null ? _messageTemplateService.GetTemplate(part.Record.LayoutId.Value) : null); part.LayoutField.Setter(x => { part.Record.LayoutId = x != null ? x.Id : default(int?); return x; }); }
public abstract string ParseTemplate(MessageTemplatePart template, ParseTemplateContext context);
public string ParseTemplate(MessageTemplatePart template, ParseTemplateContext context) { var parser = SelectParser(template); return(parser.ParseTemplate(template, context)); }
public string ParseTemplate(MessageTemplatePart template, ParseTemplateContext context) { var parser = SelectParser(template); return parser.ParseTemplate(template, context); }