Exemplo n.º 1
0
 private static void RegisterOddEvenHelper(HandlebarsDotNet.IHandlebars hbs)
 {
     hbs.RegisterHelper("odd", (writer, options, context, arguments) =>
     {
         int number = 0;
         if (arguments.Length == 1 && int.TryParse(arguments[0].ToString(), out number) && number % 2 != 0)
         {
             options.Template(writer, (object)context);
         }
         else
         {
             options.Inverse(writer, (object)context);
         }
     });
     hbs.RegisterHelper("even", (writer, options, context, arguments) =>
     {
         int number = 0;
         if (arguments.Length == 1 && int.TryParse(arguments[0].ToString(), out number) && number % 2 == 0)
         {
             options.Template(writer, (object)context);
         }
         else
         {
             options.Inverse(writer, (object)context);
         }
     });
 }
Exemplo n.º 2
0
 /// <summary>
 /// A block helper.
 /// Returns nothing, executes the template-part if contidions are met.
 /// </summary>
 /// <param name="hbs">The HBS.</param>
 private void RegisterEqualHelper(HandlebarsDotNet.IHandlebars hbs)
 {
     hbs.RegisterHelper("equal", (writer, options, context, arguments) =>
     {
         if (arguments.Length == 2 && arguments[0].Equals(arguments[1]))
         {
             options.Template(writer, (object)context);
         }
         else
         {
             options.Inverse(writer, (object)context);
         }
     });
     hbs.RegisterHelper("equaldate", (writer, options, context, arguments) =>
     {
         try
         {
             DateTime datetime1 = DateTime.Parse(arguments[0].ToString(), null, System.Globalization.DateTimeStyles.RoundtripKind);
             DateTime datetime2 = DateTime.Parse(arguments[1].ToString(), null, System.Globalization.DateTimeStyles.RoundtripKind);
             if (datetime1.Date == datetime2.Date)
             {
                 options.Template(writer, (object)context);
             }
             else
             {
                 options.Inverse(writer, (object)context);
             }
         }
         catch (Exception)
         {
             options.Inverse(writer, (object)context);
         }
     });
 }
Exemplo n.º 3
0
        private void RegisterRegisterScriptHelper(HandlebarsDotNet.IHandlebars hbs, Page page, string sourceFolder)
        {
            hbs.RegisterHelper("registerscript", (writer, context, parameters) =>
            {
                if (parameters.Length == 1)
                {
                    string jsfilename = parameters[0].ToString();
                    DnnUtils.RegisterScript(page, sourceFolder, jsfilename, _jsOrder);
                    _jsOrder++;
                }
            });
            hbs.RegisterHelper("registerform", (writer, context, parameters) =>
            {
                string view = "bootstrap";
                if (parameters.Length == 1)
                {
                    view = parameters[0].ToString();
                }

                FormHelpers.RegisterForm(page, sourceFolder, view, ref _jsOrder);
            });

            hbs.RegisterHelper("registereditform", (writer, context, parameters) =>
            {
                string prefix = "";
                if (parameters.Length == 1)
                {
                    prefix = parameters[0].ToString();
                }
                FormHelpers.RegisterEditForm(page, sourceFolder, PortalSettings.Current.PortalId, prefix, ref _jsOrder);
            });
        }
Exemplo n.º 4
0
 private static void RegisterUrlHelper(HandlebarsDotNet.IHandlebars hbs)
 {
     hbs.RegisterHelper("prefixurl", (writer, context, parameters) =>
     {
         try
         {
             string url      = parameters[0].ToString();
             string lowerUrl = url.ToLower();
             if (!lowerUrl.StartsWith("http://") &&
                 !lowerUrl.StartsWith("https://") &&
                 !lowerUrl.StartsWith("tel:") &&
                 !lowerUrl.StartsWith("mail:"))
             {
                 if (IsEmailAdress(url))
                 {
                     url = "mailto:" + url;
                 }
                 else if (IsPhoneNumber(url))
                 {
                     url = "tel:" + url;
                 }
                 else
                 {
                     url = "http://" + url;
                 }
             }
             writer.WriteSafeString(url);
         }
         catch (Exception)
         {
             writer.WriteSafeString("");
         }
     });
 }
Exemplo n.º 5
0
 private static void RegisterEmailHelper(HandlebarsDotNet.IHandlebars hbs)
 {
     hbs.RegisterHelper("protectemail", (writer, context, parameters) =>
     {
         try
         {
             string email       = parameters[0].ToString();
             string subject     = "";
             string visibleText = "";
             if (parameters.Length > 1)
             {
                 subject = parameters[1].ToString();
             }
             if (parameters.Length > 2)
             {
                 visibleText = parameters[2].ToString();
             }
             writer.WriteSafeString(email.ProtectEmail(subject, visibleText));
         }
         catch (Exception)
         {
             writer.WriteSafeString("");
         }
     });
 }
Exemplo n.º 6
0
 private static void RegisterRawHelper(HandlebarsDotNet.IHandlebars hbs)
 {
     hbs.RegisterHelper("raw", (writer, options, context, parameters) =>
     {
         options.Template(writer, null);
     });
 }
Exemplo n.º 7
0
        /*
         * private void RegisterEditUrlHelper(HandlebarsDotNet.IHandlebars hbs, IModuleControl module)
         * {
         *  hbs.RegisterHelper("editurl", (writer, context, parameters) =>
         *  {
         *      if (parameters.Length == 1)
         *      {
         *          string id = parameters[0] as string;
         *          writer.WriteSafeString(module.ModuleContext.EditUrl("itemid", id));
         *      }
         *  });
         * }
         */

        /// <summary>
        /// Retrieves image URL.
        /// Param1 is imageId,
        /// Param2 is Size of the image (in Bootstrap 12th),
        /// Param3 is ratio string (eg '1x1'),
        /// </summary>
        /// <param name="hbs">The HBS.</param>
        private void RegisterImageUrlHelper(HandlebarsDotNet.IHandlebars hbs)
        {
            hbs.RegisterHelper("imageurl", (writer, context, parameters) =>
            {
                if (parameters.Length == 3)
                {
                    string imageId     = parameters[0] as string;
                    int width          = Normalize.DynamicValue(parameters[1], -1);
                    string ratiostring = parameters[2] as string;
                    bool isMobile      = HttpContext.Current.Request.Browser.IsMobileDevice;

                    ImageUri imageObject = ImageUriFactory.CreateImageUri(imageId);
                    var imageUrl         = imageObject == null ? string.Empty : imageObject.GetImageUrl(width, ratiostring, isMobile);

                    writer.WriteSafeString(imageUrl);
                }
                if (parameters.Length == 2)
                {
                    string imageId = parameters[0] as string;
                    int width      = Normalize.DynamicValue(parameters[1], -1);
                    bool isMobile  = HttpContext.Current.Request.Browser.IsMobileDevice;

                    ImageUri imageObject = ImageUriFactory.CreateImageUri(imageId);
                    var imageUrl         = imageObject == null ? string.Empty : imageObject.GetImageUrl(width, imageObject.RawRatio, isMobile);

                    writer.WriteSafeString(imageUrl);
                }
            });
        }
Exemplo n.º 8
0
        /// <summary>
        /// Retrieved an element from a list.
        /// First param is List, the second param is the int with the position to retrieve.
        /// Zero-based retrieval
        /// </summary>
        /// <param name="hbs">The HBS.</param>
        private void RegisterArrayIndexHelper(HandlebarsDotNet.IHandlebars hbs)
        {
            hbs.RegisterHelper("arrayindex", (writer, context, parameters) =>
            {
                try
                {
                    object[] a;
                    if (parameters[0] is IEnumerable <object> )
                    {
                        var en = parameters[0] as IEnumerable <object>;
                        a      = en.ToArray();
                    }
                    else
                    {
                        a = (object[])parameters[0];
                    }

                    int b    = int.Parse(parameters[1].ToString());
                    object c = a[b];
                    HandlebarsDotNet.HandlebarsExtensions.WriteSafeString(writer, c.ToString());
                }
                catch (Exception)
                {
                    HandlebarsDotNet.HandlebarsExtensions.WriteSafeString(writer, "");
                }
            });
        }
Exemplo n.º 9
0
        private void RegisterFormatNumberHelper(HandlebarsDotNet.IHandlebars hbs)
        {
            hbs.RegisterHelper("formatNumber", (writer, context, parameters) =>
            {
                try
                {
                    decimal?number  = parameters[0] as decimal?;
                    string format   = parameters[1].ToString();
                    string provider = parameters[2].ToString();

                    IFormatProvider formatprovider = null;
                    if (provider.ToLower() == "invariant")
                    {
                        formatprovider = CultureInfo.InvariantCulture;
                    }
                    else if (!string.IsNullOrWhiteSpace(provider))
                    {
                        formatprovider = CultureInfo.CreateSpecificCulture(provider);
                    }

                    string res = number.Value.ToString(format, formatprovider);
                    HandlebarsDotNet.HandlebarsExtensions.WriteSafeString(writer, res);
                }
                catch (Exception)
                {
                    HandlebarsDotNet.HandlebarsExtensions.WriteSafeString(writer, "");
                }
            });
        }
Exemplo n.º 10
0
 private void RegisterScriptHelper(HandlebarsDotNet.IHandlebars hbs)
 {
     hbs.RegisterHelper("script", (writer, options, context, arguments) =>
     {
         HandlebarsDotNet.HandlebarsExtensions.WriteSafeString(writer, "<script>");
         options.Template(writer, (object)context);
         HandlebarsDotNet.HandlebarsExtensions.WriteSafeString(writer, "</script>");
     });
 }
Exemplo n.º 11
0
 private void RegisterHandlebarsHelper(HandlebarsDotNet.IHandlebars hbs)
 {
     hbs.RegisterHelper("handlebars", (writer, options, context, arguments) =>
     {
         HandlebarsDotNet.HandlebarsExtensions.WriteSafeString(writer, "<script id=\"jplist-templatex\" type=\"text/x-handlebars-template\">");
         HandlebarsDotNet.HandlebarsExtensions.WriteSafeString(writer, context);
         HandlebarsDotNet.HandlebarsExtensions.WriteSafeString(writer, "</script>");
     });
 }
Exemplo n.º 12
0
 private void RegisterEachPublishedHelper(HandlebarsDotNet.IHandlebars hbs)
 {
     hbs.RegisterHelper("published", (writer, options, context, parameters) =>
     {
         bool EditMode = PortalSettings.Current.UserMode == PortalSettings.Mode.Edit;
         if (EditMode)
         {
             options.Template(writer, parameters[0]);
         }
         else
         {
             var lst = new List <dynamic>();
             foreach (dynamic item in parameters[0] as IEnumerable)
             {
                 bool show = true;
                 try
                 {
                     if (item.publishstatus != "published")
                     {
                         show = false;
                     }
                 }
                 catch (Exception)
                 {
                 }
                 try
                 {
                     DateTime publishstartdate = DateTime.Parse(item.publishstartdate, null, System.Globalization.DateTimeStyles.RoundtripKind);
                     if (publishstartdate.Date >= DateTime.Today)
                     {
                         show = false;
                     }
                 }
                 catch (Exception)
                 {
                 }
                 try
                 {
                     DateTime publishenddate = DateTime.Parse(item.publishenddate, null, System.Globalization.DateTimeStyles.RoundtripKind);
                     if (publishenddate.Date <= DateTime.Today)
                     {
                         show = false;
                     }
                 }
                 catch (Exception)
                 {
                 }
                 if (show)
                 {
                     lst.Add(item);
                 }
             }
             options.Template(writer, lst);
         }
     });
 }
Exemplo n.º 13
0
        private void RegisterFormatNumberHelper(HandlebarsDotNet.IHandlebars hbs)
        {
            hbs.RegisterHelper("formatNumber", (writer, context, parameters) =>
            {
                try
                {
                    decimal number;
                    if (parameters[0] is decimal?)
                    {
                        number = (parameters[0] as decimal?).Value;
                    }
                    else if (parameters[0] is decimal)
                    {
                        number = (decimal)parameters[0];
                    }
                    else
                    {
                        number = decimal.Parse(parameters[0].ToString());
                    }

                    string res    = "";
                    string format = "0.00";

                    if (parameters.Count() > 1)
                    {
                        format = parameters[1].ToString();
                    }
                    if (parameters.Count() > 2 && !string.IsNullOrWhiteSpace(parameters[2].ToString()))
                    {
                        string provider = parameters[2].ToString();
                        IFormatProvider formatprovider = null;
                        if (provider.ToLower() == "invariant")
                        {
                            formatprovider = CultureInfo.InvariantCulture;
                        }
                        else
                        {
                            formatprovider = CultureInfo.CreateSpecificCulture(provider);
                        }
                        res = number.ToString(format, formatprovider);
                    }
                    else
                    {
                        res = number.ToString(format);
                    }
                    //string provider = parameters[2].ToString();
                    //string res = number.Value.ToString(format, formatprovider);
                    HandlebarsDotNet.HandlebarsExtensions.WriteSafeString(writer, res);
                }
                catch (Exception)
                {
                    HandlebarsDotNet.HandlebarsExtensions.WriteSafeString(writer, "");
                }
            });
        }
Exemplo n.º 14
0
 private void RegisterRegisterScriptHelper(HandlebarsDotNet.IHandlebars hbs, Page page, string sourceFolder)
 {
     hbs.RegisterHelper("registerscript", (writer, context, parameters) =>
     {
         if (parameters.Length == 1)
         {
             string jsfilename = parameters[0].ToString();
             DnnUtils.RegisterScript(page, sourceFolder, jsfilename, _jsOrder);
             _jsOrder++;
         }
     });
 }
Exemplo n.º 15
0
 /// <summary>
 /// A block helper.
 /// Returns nothing, executes the template-part if contidions are met.
 /// </summary>
 /// <param name="hbs">The HBS.</param>
 private void RegisterEqualHelper(HandlebarsDotNet.IHandlebars hbs)
 {
     hbs.RegisterHelper("equal", (writer, options, context, arguments) =>
     {
         if (arguments.Length == 2 && arguments[0].Equals(arguments[1]))
         {
             options.Template(writer, (object)context);
         }
         else
         {
             options.Inverse(writer, (object)context);
         }
     });
 }
Exemplo n.º 16
0
 private void RegisterConvertToJsonHelper(HandlebarsDotNet.IHandlebars hbs)
 {
     hbs.RegisterHelper("convertToJson", (writer, context, parameters) =>
     {
         try
         {
             var res = System.Web.Helpers.Json.Encode(parameters[0]);
             HandlebarsDotNet.HandlebarsExtensions.WriteSafeString(writer, res);
         }
         catch (Exception)
         {
             HandlebarsDotNet.HandlebarsExtensions.WriteSafeString(writer, "");
         }
     });
 }
Exemplo n.º 17
0
 private void RegisterRegisterStylesheetHelper(HandlebarsDotNet.IHandlebars hbs, Page page, string sourceFolder)
 {
     hbs.RegisterHelper("registerstylesheet", (writer, context, parameters) =>
     {
         if (parameters.Length == 1)
         {
             string cssfilename = parameters[0].ToString();
             if (!cssfilename.StartsWith("/") && !cssfilename.Contains("//"))
             {
                 cssfilename = sourceFolder + cssfilename;
             }
             ClientResourceManager.RegisterStyleSheet(page, page.ResolveUrl(cssfilename), FileOrder.Css.PortalCss);
         }
     });
 }
Exemplo n.º 18
0
 private void RegisterConvertHtmlToTextHelper(HandlebarsDotNet.IHandlebars hbs)
 {
     hbs.RegisterHelper("convertHtmlToText", (writer, context, parameters) =>
     {
         try
         {
             string html = parameters[0].ToString();
             string res  = DotNetNuke.Services.Mail.Mail.ConvertToText(html);
             HandlebarsDotNet.HandlebarsExtensions.WriteSafeString(writer, res);
         }
         catch (Exception)
         {
             HandlebarsDotNet.HandlebarsExtensions.WriteSafeString(writer, "");
         }
     });
 }
Exemplo n.º 19
0
 private void RegisterReplaceNewlineHelper(HandlebarsDotNet.IHandlebars hbs)
 {
     hbs.RegisterHelper("replacenewline", (writer, context, parameters) =>
     {
         try
         {
             string text = parameters[0].ToString();
             text        = text.Replace("\n", parameters[1].ToString());
             HandlebarsDotNet.HandlebarsExtensions.WriteSafeString(writer, text);
         }
         catch (Exception)
         {
             HandlebarsDotNet.HandlebarsExtensions.WriteSafeString(writer, "");
         }
     });
 }
Exemplo n.º 20
0
 private void RegisterSubstractionHelper(HandlebarsDotNet.IHandlebars hbs)
 {
     hbs.RegisterHelper("substract", (writer, context, parameters) =>
     {
         try
         {
             int a = int.Parse(parameters[0].ToString());
             int b = int.Parse(parameters[1].ToString());
             int c = a - b;
             HandlebarsDotNet.HandlebarsExtensions.WriteSafeString(writer, c.ToString());
         }
         catch (Exception)
         {
             HandlebarsDotNet.HandlebarsExtensions.WriteSafeString(writer, "0");
         }
     });
 }
Exemplo n.º 21
0
 private static void RegisterReplaceHelper(HandlebarsDotNet.IHandlebars hbs)
 {
     hbs.RegisterHelper("replace", (writer, context, parameters) =>
     {
         try
         {
             string text      = parameters[0].ToString();
             string oldString = parameters[1].ToString().Replace("\\n", "\n");
             text             = text.Replace(oldString, parameters[2].ToString());
             writer.WriteSafeString(text);
         }
         catch (Exception)
         {
             writer.WriteSafeString("");
         }
     });
 }
Exemplo n.º 22
0
 private static void RegisterAdditionHelper(HandlebarsDotNet.IHandlebars hbs)
 {
     hbs.RegisterHelper("add", (writer, context, parameters) =>
     {
         try
         {
             int a = int.Parse(parameters[0].ToString());
             int b = int.Parse(parameters[1].ToString());
             int c = a + b;
             writer.WriteSafeString(c.ToString());
         }
         catch (Exception)
         {
             writer.WriteSafeString("0");
         }
     });
 }
Exemplo n.º 23
0
 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("");
         }
     });
 }
Exemplo n.º 24
0
        private void RegisterFormatDateTimeHelper(HandlebarsDotNet.IHandlebars hbs)
        {
            hbs.RegisterHelper("formatDateTime", (writer, context, parameters) =>
            {
                try
                {
                    string res;
                    //DateTime? datetime = parameters[0] as DateTime?;

                    DateTime datetime = DateTime.Parse(parameters[0].ToString(), null, System.Globalization.DateTimeStyles.RoundtripKind);
                    string format     = "dd/MM/yyyy";
                    if (parameters.Count() > 1)
                    {
                        format = parameters[1].ToString();
                    }
                    if (parameters.Count() > 2 && !string.IsNullOrWhiteSpace(parameters[2].ToString()))
                    {
                        string provider = parameters[2].ToString();
                        IFormatProvider formatprovider = null;
                        if (provider.ToLower() == "invariant")
                        {
                            formatprovider = CultureInfo.InvariantCulture;
                        }
                        else
                        {
                            formatprovider = CultureInfo.CreateSpecificCulture(provider);
                        }
                        res = datetime.ToString(format, formatprovider);
                    }
                    else
                    {
                        res = datetime.ToString(format);
                    }

                    HandlebarsDotNet.HandlebarsExtensions.WriteSafeString(writer, res);
                }
                catch (Exception)
                {
                    HandlebarsDotNet.HandlebarsExtensions.WriteSafeString(writer, "");
                }
            });
        }
Exemplo n.º 25
0
        private static void RegisterConvertHtmlToTextHelper(HandlebarsDotNet.IHandlebars hbs)
        {
            hbs.RegisterHelper("convertHtmlToText", (writer, context, parameters) =>
            {
                try
                {
                    string html = parameters[0].ToString();
                    //string res = DotNetNuke.Services.Mail.Mail.ConvertToText(html);

                    var formattedHtml = HtmlUtils.FormatText(html, true);
                    //var styleLessHtml = HtmlUtils.RemoveInlineStyle(formattedHtml);
                    var styleLessHtml = RemoveInlineStylesRegEx.Replace(formattedHtml, string.Empty);
                    string res        = HtmlUtils.StripTags(styleLessHtml, true);

                    writer.WriteSafeString(res);
                }
                catch (Exception)
                {
                    writer.WriteSafeString("");
                }
            });
        }
Exemplo n.º 26
0
 private void RegisterTruncateWordsHelper(HandlebarsDotNet.IHandlebars hbs)
 {
     hbs.RegisterHelper("truncateWords", (writer, context, parameters) =>
     {
         try
         {
             string html         = parameters[0].ToString();
             int maxCharacters   = int.Parse(parameters[1].ToString());
             string trailingText = "";
             if (parameters.Count() > 2)
             {
                 trailingText = parameters[2].ToString();
             }
             string res = html.TruncateWords(maxCharacters, trailingText);
             HandlebarsDotNet.HandlebarsExtensions.WriteSafeString(writer, res);
         }
         catch (Exception)
         {
             HandlebarsDotNet.HandlebarsExtensions.WriteSafeString(writer, "");
         }
     });
 }
Exemplo n.º 27
0
        /// <summary>
        /// Translate an enum value
        /// parameters[0] = enum array (eg from schema)
        /// parameters[1] = label option array (from option file)
        /// parameters[2] = selected enum value
        /// </summary>
        /// <param name="hbs">The HBS.</param>
        private void RegisterArrayTranslateHelper(HandlebarsDotNet.IHandlebars hbs)
        {
            hbs.RegisterHelper("arraytranslate", (writer, context, parameters) =>
            {
                try
                {
                    object[] a;
                    if (parameters[0] is IEnumerable <object> )
                    {
                        var en = parameters[0] as IEnumerable <object>;
                        a      = en.ToArray();
                    }
                    else
                    {
                        a = (object[])parameters[0];
                    }
                    object[] b;
                    if (parameters[1] is IEnumerable <object> )
                    {
                        var en = parameters[1] as IEnumerable <object>;
                        b      = en.ToArray();
                    }
                    else
                    {
                        b = (object[])parameters[1];
                    }
                    string c = parameters[2].ToString();
                    int i    = Array.IndexOf(a, c);

                    object res = b[i];
                    HandlebarsDotNet.HandlebarsExtensions.WriteSafeString(writer, res.ToString());
                }
                catch (Exception)
                {
                    HandlebarsDotNet.HandlebarsExtensions.WriteSafeString(writer, "");
                }
            });
        }
Exemplo n.º 28
0
 private static void RegisterConvertToJsonHelper(HandlebarsDotNet.IHandlebars hbs)
 {
     hbs.RegisterHelper("convertToJson", (writer, context, parameters) =>
     {
         try
         {
             string res;
             if (parameters[0] is JToken)
             {
                 res = ((JToken)parameters[0]).ToString();
             }
             else
             {
                 res = System.Web.Helpers.Json.Encode(parameters[0]);
             }
             writer.WriteSafeString(res);
         }
         catch (Exception)
         {
             writer.WriteSafeString("");
         }
     });
 }
Exemplo n.º 29
0
        private void RegisterFormatDateTimeHelper(HandlebarsDotNet.IHandlebars hbs)
        {
            hbs.RegisterHelper("formatDateTime", (writer, context, parameters) =>
            {
                // parameters : datetime iso string, format, culture
                try
                {
                    string res;
                    DateTime datetime = DateTime.Parse(parameters[0].ToString(), null, System.Globalization.DateTimeStyles.RoundtripKind);
                    string format     = "dd/MM/yyyy";
                    if (parameters.Count() > 1)
                    {
                        format = parameters[1].ToString();
                    }
                    if (parameters.Count() > 2 && !string.IsNullOrWhiteSpace(parameters[2].ToString()))
                    {
                        string provider = parameters[2].ToString();
                        IFormatProvider formatprovider = null;
                        if (provider.ToLower() == "invariant")
                        {
                            formatprovider = CultureInfo.InvariantCulture;
                        }
                        else
                        {
                            formatprovider = CultureInfo.CreateSpecificCulture(provider);
                        }
                        res = datetime.ToString(format, formatprovider);
                    }
                    else
                    {
                        res = datetime.ToString(format);
                    }

                    HandlebarsDotNet.HandlebarsExtensions.WriteSafeString(writer, res);
                }
                catch (Exception)
                {
                    HandlebarsDotNet.HandlebarsExtensions.WriteSafeString(writer, "");
                }
            });

            /*
             * hbs.RegisterHelper("format2DateTime", (writer, context, parameters) =>
             * {
             *  // parameters : start datetime iso string, end datetime iso string, date time separator, dates separator, dateformat, time format, culture
             *  try
             *  {
             *      string res;
             *      DateTime datetime1 = DateTime.Parse(parameters[0].ToString(), null, System.Globalization.DateTimeStyles.RoundtripKind);
             *
             *      string datetimeSeparator = " ";
             *      string datesSeparator = " ";
             *      string dateformat = "dd/MM/yyyy";
             *      string timeformat = "hh:mm";
             *      if (parameters.Count() > 2)
             *      {
             *          datetimeSeparator = parameters[2].ToString();
             *      }
             *      if (parameters.Count() > 3)
             *      {
             *          datesSeparator = parameters[3].ToString();
             *      }
             *      if (parameters.Count() > 4)
             *      {
             *          dateformat = parameters[4].ToString();
             *      }
             *      if (parameters.Count() > 5)
             *      {
             *          timeformat = parameters[5].ToString();
             *      }
             *      Func< DateTime, string, string> formatDT = (datetime, format) =>
             *      {
             *          if (parameters.Count() > 6 && !string.IsNullOrWhiteSpace(parameters[6].ToString()))
             *          {
             *              string provider = parameters[6].ToString();
             *              IFormatProvider formatprovider = null;
             *              if (provider.ToLower() == "invariant")
             *              {
             *                  formatprovider = CultureInfo.InvariantCulture;
             *              }
             *              else
             *              {
             *                  formatprovider = CultureInfo.CreateSpecificCulture(provider);
             *              }
             *              return datetime.ToString(format, formatprovider);
             *          }
             *          else
             *          {
             *              return datetime.ToString(format);
             *          }
             *      };
             *      res = formatDT(datetime1, dateformat);
             *      if (datetime1.TimeOfDay.Ticks > 0)
             *      {
             *          res += datetimeSeparator;
             *          res += formatDT(datetime1, timeformat);
             *      }
             *      if (!string.IsNullOrEmpty(parameters[1].ToString()))
             *      {
             *          DateTime datetime2 = DateTime.Parse(parameters[1].ToString(), null, System.Globalization.DateTimeStyles.RoundtripKind);
             *          res += datesSeparator;
             *          if (datetime1.Date != datetime2.Date)
             *          {
             *              res += formatDT(datetime2, dateformat);
             *          }
             *          if (datetime2.TimeOfDay.Ticks > 0)
             *          {
             *              res += datetimeSeparator;
             *              res += formatDT(datetime2, timeformat);
             *          }
             *      }
             *      HandlebarsDotNet.HandlebarsExtensions.WriteSafeString(writer, res);
             *  }
             *  catch (Exception)
             *  {
             *      HandlebarsDotNet.HandlebarsExtensions.WriteSafeString(writer, "");
             *  }
             * });
             */
        }
Exemplo n.º 30
0
        /// <summary>
        /// Registers the array lookup helper.
        /// arguments[0] =
        /// arguments[1] =
        /// arguments[2] =
        /// </summary>
        /// <param name="hbs">The HBS.</param>
        private void RegisterArrayLookupHelper(HandlebarsDotNet.IHandlebars hbs)
        {
            hbs.RegisterHelper("lookup", (writer, options, context, arguments) =>
            {
                object[] arr;
                if (arguments[0] is IEnumerable <object> )
                {
                    var en = arguments[0] as IEnumerable <object>;
                    arr    = en.ToArray();
                }
                else
                {
                    arr = (object[])arguments[0];
                }

                var field = arguments[1].ToString();
                var value = arguments[2].ToString();
                foreach (var arrayItem in arr)
                {
                    object member = DynamicUtils.GetMemberValue(arrayItem, field);
                    if (value.Equals(member))
                    {
                        options.Template(writer, (object)arrayItem);
                    }
                }
                options.Inverse(writer, (object)context);
            });

            /*
             * hbs.RegisterHelper("arraylookup", (writer, context, parameters) =>
             * {
             *  try
             *  {
             *      object[] arr;
             *      if (parameters[0] is IEnumerable<Object>)
             *      {
             *          var en = parameters[0] as IEnumerable<Object>;
             *          arr = en.ToArray();
             *      }
             *      else
             *      {
             *          arr = (object[])parameters[0];
             *      }
             *
             *      var value = parameters[1].ToString();
             *      var text = parameters[2].ToString();
             *      var key = parameters[3].ToString();
             *      foreach (var item in collection)
             *      {
             *
             *      }
             *      object res = b[i];
             *      string c = parameters[2].ToString();
             *      HandlebarsDotNet.HandlebarsExtensions.
             *      //HandlebarsDotNet.HandlebarsExtensions.WriteSafeString(writer, res.ToString());
             *  }
             *  catch (Exception)
             *  {
             *      HandlebarsDotNet.HandlebarsExtensions.WriteSafeString(writer, "");
             *  }
             * });
             */
        }