Ejemplo n.º 1
0
        public override void Render(IParrotWriter writer, IRendererFactory rendererFactory, Statement statement, IDictionary <string, object> documentHost, object model)
        {
            var localModel = GetLocalModel(documentHost, statement, model);

            //Assert that we're looping over something
            IEnumerable loop = localModel as IEnumerable;

            if (loop == null)
            {
                throw new InvalidCastException("model is not IEnumerable");
            }

            //create a local copy
            IList <object> items = ToList(loop);

            //create locals object to handle local values to the method
            Locals locals = new Locals(documentHost);

            for (int i = 0; i < items.Count; i++)
            {
                var item = items[i];

                foreach (var child in statement.Children)
                {
                    var renderer = rendererFactory.GetRenderer(child.Name);
                    locals.Push(IteratorItem(i, items));

                    renderer.Render(writer, rendererFactory, child, documentHost, item);

                    locals.Pop();
                }
            }
        }
Ejemplo n.º 2
0
        public override void RenderChildren(IParrotWriter writer, Nodes.Statement statement, IRendererFactory rendererFactory, IDictionary <string, object> documentHost, object model, string defaultTag = null)
        {
            if (string.IsNullOrEmpty(defaultTag))
            {
                defaultTag = DefaultChildTag;
            }

            //get model from parameter
            //required - do not remove
            if (statement.Parameters != null && statement.Parameters.Count == 1)
            {
                var localModel = GetLocalModel(documentHost, statement, model);

                if (localModel is IEnumerable)
                {
                    //create locals object to handle local values to the method
                    Locals locals = new Locals(documentHost);

                    IList <object> items = ToList(model as IEnumerable);
                    for (int i = 0; i < items.Count; i++)
                    {
                        var localItem = items[i];
                        locals.Push(IteratorItem(i, items));

                        base.RenderChildren(writer, statement.Children, rendererFactory, documentHost, defaultTag, localItem);

                        locals.Pop();
                    }
                }
            }
            else
            {
                base.RenderChildren(writer, statement.Children, rendererFactory, documentHost, defaultTag, model);
            }
        }
        void EndSimple(ILGenerator il)
        {
            Debug.Assert(il != null);
            Debug.Assert(ArgsCount > 0);
            Debug.Assert(ArgsCount < 3);

            il_EmitSaveElem(il);

            if (ArgsCount == 2)
            {
                var array  = Locals.Pop();
                var index2 = Locals.Pop();
                var var2   = ArgsLocs[1];

                // x += step;
                il.Emit(OpCodes.Ldloc, var2);
                il_EmitLoadArg(il, 2);
                Generic.Operation(il, 1);
                il.Emit(OpCodes.Stloc, var2);

                EmitLoopEnd(il, index2, array);
            }

            var index = Locals.Pop();
            var var   = ArgsLocs[0];

            il.Emit(OpCodes.Ldloc, var);
            il_EmitLoadArg(il, 1);
            Generic.Operation(il, 1);
            il.Emit(OpCodes.Stloc, var);

            EmitLoopEnd(il, index, null);
        }
        void EndMulti(ILGenerator il)
        {
            Debug.Assert(il != null);
            Debug.Assert(ArgsCount > 2);

            il_EmitSaveElem(il);

            for (int i = 0, j = ArgsCount - 1; i < ArgsCount; i++, j--)
            {
                var index = Locals.Pop();
                var array = Locals.Pop();
                var var   = ArgsLocs[j];

                // x += xstep;
                il.Emit(OpCodes.Ldloc, var);
                il.Emit(OpCodes.Ldloc, StepLocs.Pop());
                Generic.Operation(il, 1);
                il.Emit(OpCodes.Stloc, var);

                EmitLoopEnd(il, index, array);
            }
        }