public static void RegisterHelpers(CMSDataContext db) { Handlebars.RegisterHelper("BottomBorder", (writer, context, args) => { writer.Write(CssStyle.BottomBorder); }); Handlebars.RegisterHelper("AlignTop", (writer, context, args) => { writer.Write(CssStyle.AlignTop); }); Handlebars.RegisterHelper("AlignRight", (writer, context, args) => { writer.Write(CssStyle.AlignRight); }); Handlebars.RegisterHelper("DataLabelStyle", (writer, context, args) => { writer.Write(CssStyle.DataLabelStyle); }); Handlebars.RegisterHelper("LabelStyle", (writer, context, args) => { writer.Write(CssStyle.LabelStyle); }); Handlebars.RegisterHelper("DataStyle", (writer, context, args) => { writer.Write(CssStyle.DataStyle); }); Handlebars.RegisterHelper("ServerLink", (writer, context, args) => { writer.Write(db.ServerLink().TrimEnd('/')); }); Handlebars.RegisterHelper("FmtZip", (writer, context, args) => { writer.Write(args[0].ToString().FmtZip()); }); Handlebars.RegisterHelper("IfEqual", (writer, options, context, args) => { if (IsEqual(args)) { options.Template(writer, (object)context); } else { options.Inverse(writer, (object)context); } }); Handlebars.RegisterHelper("IfNotEqual", (writer, options, context, args) => { if (!IsEqual(args)) { options.Template(writer, (object)context); } else { options.Inverse(writer, (object)context); } }); Handlebars.RegisterHelper("GetToken", (writer, context, args) => { var s = args[0].ToString(); var n = args[1].ToInt(); var ntoks = args.Length > 2 ? args[2].ToInt() : 2; var sep = args.Length > 3 ? args[3].ToString() : " "; var a = s.SplitStr(sep, ntoks); writer.Write(a[n].trim()); }); // Format helper in form of: {{Fmt value "fmt"}} // ex. {{Fmt Total "C"}} // fmt is required. Uses standard/custom dotnet format strings Handlebars.RegisterHelper("Fmt", (writer, context, args) => { var fmt = $"{{0:{args[1]}}}"; writer.Write(fmt, args[0]); }); // FmtPhone helper in form of: {{FmtPhone phone# "prefix"}} Handlebars.RegisterHelper("FmtPhone", (writer, context, args) => { writer.Write(args[0].ToString().FmtFone($"{args[1]}")); }); Handlebars.RegisterHelper("ReplaceCode", (writer, context, args) => { EmailReplacements r = context.Replacements as EmailReplacements ?? (context.Replacements = new EmailReplacements(db)); var code = args[0].ToString(); var p = db.LoadPersonById(args[1].ToInt()); int?oid = null; if (args.Length == 3) { oid = args[2].ToInt2(); } writer.Write(r.RenderCode(code, p, oid)); }); }
public static IHandlebars RegisterHelpers(CMSDataContext db, PythonModel pm = null, IHandlebars handlebars = null) { handlebars = handlebars ?? Handlebars.Create(); handlebars.RegisterHelper("BottomBorder", (writer, context, args) => { writer.Write(CssStyle.BottomBorder); }); handlebars.RegisterHelper("AlignTop", (writer, context, args) => { writer.Write(CssStyle.AlignTop); }); handlebars.RegisterHelper("AlignRight", (writer, context, args) => { writer.Write(CssStyle.AlignRight); }); handlebars.RegisterHelper("DataLabelStyle", (writer, context, args) => { writer.Write(CssStyle.DataLabelStyle); }); handlebars.RegisterHelper("LabelStyle", (writer, context, args) => { writer.Write(CssStyle.LabelStyle); }); handlebars.RegisterHelper("DataStyle", (writer, context, args) => { writer.Write(CssStyle.DataStyle); }); handlebars.RegisterHelper("ServerLink", (writer, context, args) => { writer.Write(db.ServerLink().TrimEnd('/')); }); handlebars.RegisterHelper("FmtZip", (writer, context, args) => { writer.Write(args[0].ToString().FmtZip()); }); handlebars.RegisterHelper("HtmlComment", (writer, context, args) => { #if DEBUG writer.Write($"<h6>{args[0].ToString()} {args[1].ToString()}</h6>"); #else writer.Write($"<!--{args[0].ToString()} {args[1].ToString()}-->"); #endif }); handlebars.RegisterHelper("IfEqual", (writer, options, context, args) => { if (IsEqual(args)) { options.Template(writer, (object)context); } else { options.Inverse(writer, (object)context); } }); handlebars.RegisterHelper("IfNotEqual", (writer, options, context, args) => { if (!IsEqual(args)) { options.Template(writer, (object)context); } else { options.Inverse(writer, (object)context); } }); handlebars.RegisterHelper("IfCond", (writer, options, context, args) => { var op = HttpUtility.HtmlDecode(args[1].ToString()); bool b = false; switch (op) { case "==": b = Compare(args) == 0; break; case "!=": b = Compare(args) != 0; break; case "<": b = Compare(args) < 0; break; case ">": b = Compare(args) > 0; break; case ">=": b = Compare(args) >= 0; break; case "<=": b = Compare(args) <= 0; break; case "&&": b = NumTrue(args) == 2; break; case "||": b = NumTrue(args) >= 1; break; } if (b) { options.Template(writer, (object)context); } else { options.Inverse(writer, (object)context); } }); handlebars.RegisterHelper("IfGT", (writer, options, context, args) => { if (Compare2(args) > 0) { options.Template(writer, (object)context); } else { options.Inverse(writer, (object)context); } }); handlebars.RegisterHelper("IfLT", (writer, options, context, args) => { if (Compare2(args) < 0) { options.Template(writer, (object)context); } else { options.Inverse(writer, (object)context); } }); handlebars.RegisterHelper("IfGE", (writer, options, context, args) => { if (Compare2(args) <= 0) { options.Template(writer, (object)context); } else { options.Inverse(writer, (object)context); } }); handlebars.RegisterHelper("IfLE", (writer, options, context, args) => { if (Compare2(args) <= 0) { options.Template(writer, (object)context); } else { options.Inverse(writer, (object)context); } }); handlebars.RegisterHelper("GetToken", (writer, context, args) => { var s = args[0].ToString(); var n = args[1].ToInt(); var ntoks = args.Length > 2 ? args[2].ToInt() : 2; var sep = args.Length > 3 ? args[3].ToString() : " "; var a = s.SplitStr(sep, ntoks); writer.Write(a[n]?.Trim() ?? ""); }); handlebars.RegisterHelper("FmtMDY", (writer, context, args) => { DateTime dt; var s = args[0].ToString(); if (DateTime.TryParse(s, out dt)) { writer.Write(dt.ToShortDateString()); } }); handlebars.RegisterHelper("FmtDate", (writer, context, args) => { DateTime dt; var s = args[0].ToString(); if (DateTime.TryParse(s, out dt)) { writer.Write(dt.ToShortDateString()); } }); handlebars.RegisterHelper("FmtMoney", (writer, context, args) => { decimal d; var s = args[0].ToString(); if (decimal.TryParse(s, out d)) { writer.Write(d.ToString("C")); } }); // Format helper in form of: {{Fmt value "fmt"}} // ex. {{Fmt Total "C"}} // fmt is required. Uses standard/custom dotnet format strings handlebars.RegisterHelper("Fmt", (writer, context, args) => { var fmt = $"{{0:{args[1]}}}"; writer.Write(fmt, args[0]); }); // FmtPhone helper in form of: {{FmtPhone phone# "prefix"}} handlebars.RegisterHelper("FmtPhone", (writer, context, args) => { writer.Write(args[0].ToString().FmtFone($"{args[1]}")); }); handlebars.RegisterHelper("ReplaceCode", (writer, context, args) => { EmailReplacements r = context.Replacements as EmailReplacements ?? (context.Replacements = new EmailReplacements(db)); var code = args[0].ToString(); var p = db.LoadPersonById(args[1].ToInt()); int?oid = null; if (args.Length == 3) { oid = args[2].ToInt2(); } writer.Write(r.RenderCode(code, p, oid)); }); handlebars.RegisterHelper("Json", (writer, options, context, args) => { dynamic a = JsonDeserialize2(args[0].ToString()); foreach (var item in a) { options.Template(writer, item); } }); handlebars.RegisterHelper("Calc", (writer, context, args) => { var calcAmt = args[0].ToDouble() - args[1].ToDouble(); var calcAmtfmt = $"{{0:{'c'}}}"; writer.Write(calcAmtfmt, calcAmt); }); handlebars.RegisterHelper("ThrowError", (writer, context, args) => { throw new Exception("ThrowError called in Handlebars Helper"); }); return(handlebars); }