示例#1
0
 private static void Iterate(
     ObjectEnumeratorBindingContext context,
     IDynamicMetaObjectProvider target,
     Action <TextWriter, object> template,
     Action <TextWriter, object> ifEmpty)
 {
     if (HandlebarsUtils.IsTruthy(target))
     {
         context.Index = 0;
         var meta = target.GetMetaObject(Expression.Constant(target));
         foreach (var name in meta.GetDynamicMemberNames())
         {
             context.Key = name;
             var value = GetProperty(target, name);
             context.First = (context.Index == 0);
             template(context.TextWriter, value);
             context.Index++;
         }
         if (context.Index == 0)
         {
             ifEmpty(context.TextWriter, context.Value);
         }
     }
     else
     {
         ifEmpty(context.TextWriter, context.Value);
     }
 }
示例#2
0
        public static void Iterate(
            BindingContext context,
            EncodedTextWriter writer,
            ChainSegment[] blockParamsVariables,
            object target,
            TemplateDelegate template,
            TemplateDelegate ifEmpty)
        {
            if (!HandlebarsUtils.IsTruthy(target))
            {
                using var frame = context.CreateFrame(context.Value);
                ifEmpty(writer, frame);
                return;
            }

            if (!ObjectDescriptor.TryCreate(target, out var descriptor))
            {
                throw new HandlebarsRuntimeException($"Cannot create ObjectDescriptor for type {descriptor.DescribedType}");
            }

            if (descriptor.Iterator == null)
            {
                throw new HandlebarsRuntimeException($"Type {descriptor.DescribedType} does not support iteration");
            }

            descriptor.Iterator.Iterate(writer, context, blockParamsVariables, target, template, ifEmpty);
        }
示例#3
0
 private static void Iterate(
     ObjectEnumeratorBindingContext context,
     IEnumerable target,
     Action <TextWriter, object> template,
     Action <TextWriter, object> ifEmpty)
 {
     if (HandlebarsUtils.IsTruthy(target))
     {
         context.Index = 0;
         var keysProperty = target.GetType().GetProperty("Keys");
         if (keysProperty != null)
         {
             var keys = keysProperty.GetGetMethod().Invoke(target, null) as IEnumerable <object>;
             if (keys != null)
             {
                 foreach (var key in keys)
                 {
                     context.Key = key.ToString();
                     var value = target.GetType().GetMethod("get_Item").Invoke(target, new[] { key });
                     context.First = (context.Index == 0);
                     template(context.TextWriter, value);
                     context.Index++;
                 }
             }
         }
         if (context.Index == 0)
         {
             ifEmpty(context.TextWriter, context.Value);
         }
     }
     else
     {
         ifEmpty(context.TextWriter, context.Value);
     }
 }
示例#4
0
 private static void Iterate(
     ObjectEnumeratorBindingContext context,
     object target,
     Action <TextWriter, object> template,
     Action <TextWriter, object> ifEmpty)
 {
     if (HandlebarsUtils.IsTruthy(target))
     {
         context.Index = 0;
         foreach (MemberInfo member in target.GetType()
                  .GetProperties(BindingFlags.Instance | BindingFlags.Public).OfType <MemberInfo>()
                  .Concat(
                      target.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance)
                      ))
         {
             context.Key = member.Name;
             var value = AccessMember(target, member);
             context.First = (context.Index == 0);
             template(context.TextWriter, value);
             context.Index++;
         }
         if (context.Index == 0)
         {
             ifEmpty(context.TextWriter, context.Value);
         }
     }
     else
     {
         ifEmpty(context.TextWriter, context.Value);
     }
 }
        private static void Iterate(
            ObjectEnumeratorBindingContext context,
            object target,
            Action <TextWriter, object> template,
            Action <TextWriter, object> ifEmpty)
        {
            if (HandlebarsUtils.IsTruthy(target))
            {
                context.Index = 0;
                var targetType = target.GetType();
                var properties = targetType.GetProperties(BindingFlags.Instance | BindingFlags.Public).OfType <MemberInfo>();
                var fields     = targetType.GetFields(BindingFlags.Public | BindingFlags.Instance);
                foreach (var enumerableValue in new ExtendedEnumerable <MemberInfo>(properties.Concat(fields)))
                {
                    var member = enumerableValue.Value;
                    context.Key = member.Name;
                    var value = AccessMember(target, member);
                    context.First = enumerableValue.IsFirst;
                    context.Last  = enumerableValue.IsLast;
                    context.Index = enumerableValue.Index;

                    template(context.TextWriter, value);
                }

                if (context.Index == 0)
                {
                    ifEmpty(context.TextWriter, context.Value);
                }
            }
            else
            {
                ifEmpty(context.TextWriter, context.Value);
            }
        }
        public static void Iterate(
            BindingContext context,
            EncodedTextWriter writer,
            ChainSegment[] blockParamsVariables,
            object target,
            TemplateDelegate template,
            TemplateDelegate ifEmpty)
        {
            if (!HandlebarsUtils.IsTruthy(target))
            {
                using var frame = context.CreateFrame(context.Value);
                ifEmpty(writer, frame);
                return;
            }

            var targetType = target.GetType();

            if (!context.Configuration.ObjectDescriptorProvider.TryGetDescriptor(targetType, out var descriptor))
            {
                using var frame = context.CreateFrame(context.Value);
                ifEmpty(writer, frame);
                return;
            }

            if (!descriptor.ShouldEnumerate)
            {
                var properties = descriptor.GetProperties(descriptor, target);
                if (properties is IList <ChainSegment> propertiesList)
                {
                    IterateObjectWithStaticProperties(context, writer, blockParamsVariables, descriptor, target, propertiesList, targetType, template, ifEmpty);
                    return;
                }

                IterateObject(context, writer, descriptor, blockParamsVariables, target, properties, targetType, template, ifEmpty);
                return;
            }

            if (target is IList list)
            {
                IterateList(context, writer, blockParamsVariables, list, template, ifEmpty);
                return;
            }

            IterateEnumerable(context, writer, blockParamsVariables, (IEnumerable)target, template, ifEmpty);
        }
        private static void Iterate(
            ObjectEnumeratorBindingContext context,
            IEnumerable target,
            Action <TextWriter, object> template,
            Action <TextWriter, object> ifEmpty)
        {
            if (HandlebarsUtils.IsTruthy(target))
            {
                context.Index = 0;
                var targetType = target.GetType();
#if netstandard
                var keysProperty = targetType.GetRuntimeProperty("Keys");
#else
                var keysProperty = targetType.GetProperty("Keys");
#endif
                if (keysProperty != null)
                {
                    var keys = keysProperty.GetGetMethod().Invoke(target, null) as IEnumerable;
                    if (keys != null)
                    {
                        var getItemMethodInfo = targetType.GetMethod("get_Item");
                        var parameters        = new object[1];
                        foreach (var enumerableValue in new ExtendedEnumerable <object>(keys))
                        {
                            var key = parameters[0] = enumerableValue.Value;
                            context.Key = key.ToString();
                            var value = getItemMethodInfo.Invoke(target, parameters);
                            context.First = enumerableValue.IsFirst;
                            context.Last  = enumerableValue.IsLast;
                            context.Index = enumerableValue.Index;

                            template(context.TextWriter, value);
                        }
                    }
                }
                if (context.Index == 0)
                {
                    ifEmpty(context.TextWriter, context.Value);
                }
            }
            else
            {
                ifEmpty(context.TextWriter, context.Value);
            }
        }
示例#8
0
        public static void Iterate(BindingContext context,
                                   BlockParamsValueProvider blockParamsValueProvider,
                                   object target,
                                   Action <BindingContext, TextWriter, object> template,
                                   Action <BindingContext, TextWriter, object> ifEmpty)
        {
            if (!HandlebarsUtils.IsTruthy(target))
            {
                ifEmpty(context, context.TextWriter, context.Value);
                return;
            }

            var targetType = target.GetType();

            if (!(context.Configuration.ObjectDescriptorProvider.CanHandleType(targetType) &&
                  context.Configuration.ObjectDescriptorProvider.TryGetDescriptor(targetType, out var descriptor)))
            {
                ifEmpty(context, context.TextWriter, context.Value);
                return;
            }

            if (!descriptor.ShouldEnumerate)
            {
                var properties = descriptor.GetProperties(target);
                if (properties is IList propertiesList)
                {
                    IterateObjectWithStaticProperties(context, descriptor, blockParamsValueProvider, target, propertiesList, targetType, template, ifEmpty);
                    return;
                }

                IterateObject(context, descriptor, blockParamsValueProvider, target, properties, targetType, template, ifEmpty);
                return;
            }

            if (target is IList list)
            {
                IterateList(context, blockParamsValueProvider, list, template, ifEmpty);
                return;
            }

            IterateEnumerable(context, blockParamsValueProvider, (IEnumerable)target, template, ifEmpty);
        }
示例#9
0
        public void BlockHelperWithInversion()
        {
            string source = "{{^test input}}empty{{else}}not empty{{/test}}";

            var handlebars = Handlebars.Create();

            handlebars.RegisterHelper("test", (output, options, context, arguments) =>
            {
                if (HandlebarsUtils.IsTruthy(arguments[0]))
                {
                    options.Template(output, context);
                }
                else
                {
                    options.Inverse(output, context);
                }
            });

            var template = handlebars.Compile(source);

            Assert.Equal("empty", template(null));
            Assert.Equal("empty", template(new { otherInput = 1 }));
            Assert.Equal("not empty", template(new { input = 1 }));
        }