Exemplo n.º 1
0
        private static void IterateEnumerable(BindingContext context,
                                              BlockParamsValueProvider blockParamsValueProvider,
                                              IEnumerable target,
                                              Action <BindingContext, TextWriter, object> template,
                                              Action <BindingContext, TextWriter, object> ifEmpty)
        {
            using (var iterator = IteratorValueProvider.Create())
            {
                blockParamsValueProvider?.Configure(BlockParamsEnumerableConfiguration, iterator);

                iterator.Index = 0;
                var  enumerable = new ExtendedEnumerable <object>(target);
                bool enumerated = false;

                foreach (var enumerableValue in enumerable)
                {
                    enumerated     = true;
                    iterator.Value = enumerableValue.Value;
                    iterator.First = enumerableValue.IsFirst;
                    iterator.Last  = enumerableValue.IsLast;

                    using (var innerContext = context.CreateChildContext(iterator.Value))
                    {
                        innerContext.RegisterValueProvider(iterator);
                        template(context, context.TextWriter, innerContext);
                        innerContext.UnregisterValueProvider(iterator);
                    }
                }

                if (iterator.Index == 0 && !enumerated)
                {
                    ifEmpty(context, context.TextWriter, context.Value);
                }
            }
        }
Exemplo n.º 2
0
        private static void IterateList(BindingContext context,
                                        BlockParamsValueProvider blockParamsValueProvider,
                                        IList target,
                                        Action <BindingContext, TextWriter, object> template,
                                        Action <BindingContext, TextWriter, object> ifEmpty)
        {
            using (var iterator = IteratorValueProvider.Create())
            {
                blockParamsValueProvider?.Configure(BlockParamsEnumerableConfiguration, iterator);

                var count = target.Count;
                for (iterator.Index = 0; iterator.Index < count; iterator.Index++)
                {
                    iterator.Value = target[iterator.Index];
                    iterator.First = iterator.Index == 0;
                    iterator.Last  = iterator.Index == count - 1;

                    using (var innerContext = context.CreateChildContext(iterator.Value))
                    {
                        innerContext.RegisterValueProvider(iterator);
                        template(context, context.TextWriter, innerContext);
                        innerContext.UnregisterValueProvider(iterator);
                    }
                }

                if (iterator.Index == 0)
                {
                    ifEmpty(context, context.TextWriter, context.Value);
                }
            }
        }