private static void RegisterTemplateHelper(HandlebarsDotNet.IHandlebars hbs) { hbs.RegisterHelper("template", (writer, context, parameters) => { try { string html = parameters[0].ToString(); HandlebarsEngine hbs2 = new HandlebarsEngine(); var res = hbs2.Execute(html, parameters[1]); writer.WriteSafeString(res); } catch (Exception ex) { writer.WriteSafeString(""); } }); }
private string GenerateOutput(string Template, string dataJson, string settingsJson) { try { if (!(string.IsNullOrEmpty(Template))) { if (!File.Exists(Server.MapPath(Template))) Exceptions.ProcessModuleLoadException(this, new Exception(Template + " don't exist")); if (!string.IsNullOrEmpty(dataJson)) { //dynamic json = JValue.Parse(struc.Json); //JObject model = new JObject(); //model["Data"] = JValue.Parse(struc.Json); //model["Settings"] = JValue.Parse(Data); //dynamic model = new ExpandoObject(); //model.Data = JsonUtils.JsonToDynamic(struc.Json); dynamic model = JsonUtils.JsonToDynamic(dataJson); if (settingsJson != null) model.Settings = JsonUtils.JsonToDynamic(settingsJson); model.Context = new { ModuleId = ModuleContext.ModuleId, PortalId = ModuleContext.PortalId }; if (Path.GetExtension(Template) != ".hbs") { string webConfig = Path.GetDirectoryName(Server.MapPath(Template)); webConfig = webConfig.Remove(webConfig.LastIndexOf("\\")) + "\\web.config"; if (!File.Exists(webConfig)) { string filename = HostingEnvironment.MapPath("~/DesktopModules/OpenContent/Templates/web.config"); File.Copy(filename, webConfig); } try { var razorEngine = new RazorEngine(Template, ModuleContext, LocalResourceFile); var writer = new StringWriter(); RazorRender(razorEngine.Webpage, writer, model); return writer.ToString(); } catch (Exception ex) { Exceptions.ProcessModuleLoadException(string.Format("Error while loading template {0}", Template), this, ex); } } else { HandlebarsEngine hbEngine = new HandlebarsEngine(); return hbEngine.Execute(Page, Template, model); } } else { return ""; } } else { return ""; } } catch (Exception ex) { Exceptions.ProcessModuleLoadException(this, ex); } return ""; }
public HttpResponseMessage Submit(JObject form) { try { var res = new ResultDTO() { Message = "Form submitted." }; int ModuleId = ActiveModule.ModuleID; string jsonSettings = ActiveModule.ModuleSettings["data"] as string; if (!string.IsNullOrEmpty(jsonSettings)) { HandlebarsEngine hbs = new HandlebarsEngine(); SettingsDTO settings = JsonConvert.DeserializeObject<SettingsDTO>(jsonSettings); StringBuilder FormData = new StringBuilder(); if (form != null) { FormData.Append("<table boder=\"1\">"); foreach (var item in form.Properties()) { FormData.Append("<tr>").Append("<td>").Append(item.Name).Append("</td>").Append("<td>").Append(" : ").Append("</td>").Append("<td>").Append(item.Value).Append("</td>").Append("</tr>"); } FormData.Append("</table>"); //form["FormData"] = FormData.ToString(); } if (settings != null && settings.Notifications != null) { foreach (var notification in settings.Notifications) { try { MailAddress from = GenerateMailAddress(notification.From, notification.FromEmail, notification.FromName, notification.FromEmailField, notification.FromNameField, form); MailAddress to = GenerateMailAddress(notification.To, notification.ToEmail, notification.ToName, notification.ToEmailField, notification.ToNameField, form); MailAddress reply = null; if (!string.IsNullOrEmpty(notification.ReplyTo)) { reply = GenerateMailAddress(notification.ReplyTo, notification.ReplyToEmail, notification.ReplyToName, notification.ReplyToEmailField, notification.ReplyToNameField, form); } string body = FormData.ToString(); if (!string.IsNullOrEmpty(notification.EmailBody)) { body = hbs.Execute(notification.EmailBody, form); } string send = SendMail(from.ToString(), to.ToString(), (reply == null ? "" : reply.ToString()), notification.EmailSubject, body); if (!string.IsNullOrEmpty(send)) { res.Errors.Add("From:" + from.ToString() + " - To:" + to.ToString() + " - " + send); } } catch (Exception exc) { res.Errors.Add("Notification "+(settings.Notifications.IndexOf(notification)+1)+ " : " + exc.Message + " - " + (UserInfo.IsSuperUser ? exc.StackTrace : "")); Logger.Error(exc); } } } if (settings != null && settings.Settings != null) { res.Message = hbs.Execute(settings.Settings.Message, form); res.Tracking = settings.Settings.Tracking; } } OpenFormController ctrl = new OpenFormController(); var content = new OpenFormInfo() { ModuleId = ModuleId, Json = form.ToString(), CreatedByUserId = UserInfo.UserID, CreatedOnDate = DateTime.Now, LastModifiedByUserId = UserInfo.UserID, LastModifiedOnDate = DateTime.Now, Html = "", Title = "Form submitted - " + DateTime.Now.ToString() }; ctrl.AddContent(content); return Request.CreateResponse(HttpStatusCode.OK, res); } catch (Exception exc) { Logger.Error(exc); return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exc); } }