Exemplo n.º 1
0
        public override void Execute(RenderingContext context)
        {
            if (inner == null)
            {
                throw new Exception("Missing inner branch in Each node");
            }

            var obj = context.EvaluateObject(key);

            if (obj == null)
            {
                return;
            }

            var objType = obj.GetType();

            if (objType == typeof(int) || objType == typeof(uint) || objType == typeof(long) || objType == typeof(ulong) || objType == typeof(short) || objType == typeof(ushort))
            {
                var n = Convert.ToInt32(obj);
                obj = Enumerable.Range(1, n).ToArray();
            }

            var list = obj as IEnumerable;

            if (list != null)
            {
                context.operation = RenderingOperation.None;

                int index = 0;
                int last  = list.Count() - 1;
                foreach (var item in list)
                {
                    context.Set("index", index);
                    context.Set("first", index == 0);
                    context.Set("last", index == last);
                    context.DataStack.Add(item);
                    inner.Execute(context);
                    context.DataStack.RemoveAt(context.DataStack.Count - 1);

                    if (context.operation == RenderingOperation.Break)
                    {
                        context.operation = RenderingOperation.None;
                        break;
                    }

                    index++;
                }
            }
            else
            {
                context.DataStack.Add(obj);
                context.Set("index", 0);
                context.Set("first", true);
                context.Set("last", true);
                inner.Execute(context);
                context.DataStack.RemoveAt(context.DataStack.Count - 1);
            }
        }
Exemplo n.º 2
0
        public override void Execute(RenderingContext context)
        {
            if (inner == null)
            {
                throw new Exception("Missing inner branch in Each node");
            }

            var obj = context.EvaluateObject(key);

            if (obj == null)
            {
                return;
            }

            var list = obj as IEnumerable;

            if (list != null)
            {
                context.operation = RenderingOperation.None;

                int index = 0;
                int last  = list.Count() - 1;
                foreach (var item in list)
                {
                    context.Set("index", index);
                    context.Set("first", index == 0);
                    context.Set("last", index == last);
                    context.DataStack.Add(item);
                    inner.Execute(context);
                    context.DataStack.RemoveAt(context.DataStack.Count - 1);

                    if (context.operation == RenderingOperation.Break)
                    {
                        context.operation = RenderingOperation.None;
                        break;
                    }

                    index++;
                }
            }
            else
            {
                context.DataStack.Add(obj);
                context.Set("index", 0);
                context.Set("first", true);
                context.Set("last", true);
                inner.Execute(context);
                context.DataStack.RemoveAt(context.DataStack.Count - 1);
            }
        }
Exemplo n.º 3
0
        public override void Execute(RenderingContext context)
        {
            if (this.trueNode == null)
            {
                throw new Exception("Missing true branch in If node");
            }

            var result = context.EvaluateObject(condition);

            var isFalse = result == null || result.Equals(false) || ((result is string) && ((string)result).Length == 0);

            if (!isFalse && result is IEnumerable)
            {
                isFalse = !((IEnumerable)result).Any();
            }

            if (isFalse)
            {
                if (falseNode != null)
                {
                    falseNode.Execute(context);
                }

                return;
            }

            trueNode.Execute(context);
        }