Esempio n. 1
0
        public override void Render(Context context, TextWriter result)
        {
            // Get the template or template content and then either copy it (since it will be modified) or parse it
            IFileSystem         fileSystem         = context.Registers["file_system"] as IFileSystem ?? Template.FileSystem;
            ITemplateFileSystem templateFileSystem = fileSystem as ITemplateFileSystem;
            Template            template           = null;

            if (templateFileSystem != null)
            {
                template = templateFileSystem.GetTemplate(context, _templateName);
            }
            if (template == null)
            {
                string source = fileSystem.ReadTemplateFile(context, _templateName);
                template = Template.Parse(source);
            }

            List <Block>     parentBlocks   = FindBlocks(template.Root, null);
            List <Block>     orphanedBlocks = ((List <Block>)context.Scopes[0]["extends"]) ?? new List <Block>();
            BlockRenderState blockState     = BlockRenderState.Find(context) ?? new BlockRenderState();

            context.Stack(() =>
            {
                context["blockstate"] = blockState;         // Set or copy the block state down to this scope
                context["extends"]    = new List <Block>(); // Holds Blocks that were not found in the parent
                foreach (Block block in NodeList.OfType <Block>().Concat(orphanedBlocks))
                {
                    Block pb = parentBlocks.Find(b => b.BlockName == block.BlockName);

                    if (pb != null)
                    {
                        Block parent;
                        if (blockState.Parents.TryGetValue(block, out parent))
                        {
                            blockState.Parents[pb] = parent;
                        }
                        pb.AddParent(blockState.Parents, pb.GetNodeList(blockState));
                        blockState.NodeLists[pb] = block.GetNodeList(blockState);
                    }
                    else if (IsExtending(template))
                    {
                        ((List <Block>)context.Scopes[0]["extends"]).Add(block);
                    }
                }
                template.Render(result, RenderParameters.FromContext(context));
            });
        }
Esempio n. 2
0
        public override void Render(Context context, TextWriter result)
        {
            IFileSystem         fileSystem         = context.Registers["file_system"] as IFileSystem ?? Template.FileSystem;
            ITemplateFileSystem templateFileSystem = fileSystem as ITemplateFileSystem;
            Template            partial            = null;

            if (templateFileSystem != null)
            {
                partial = templateFileSystem.GetTemplate(context, _templateName);
            }
            if (partial == null)
            {
                string source = fileSystem.ReadTemplateFile(context, _templateName);
                partial = Template.Parse(source);
            }

            string shortenedTemplateName = _templateName.Substring(1, _templateName.Length - 2);
            object variable = context[_variableName ?? shortenedTemplateName, _variableName != null];

            context.Stack(() =>
            {
                foreach (var keyValue in _attributes)
                {
                    context[keyValue.Key] = context[keyValue.Value];
                }

                if (variable is IEnumerable)
                {
                    ((IEnumerable)variable).Cast <object>().ToList().ForEach(v =>
                    {
                        context[shortenedTemplateName] = v;
                        partial.Render(result, RenderParameters.FromContext(context, result.FormatProvider));
                    });
                    return;
                }

                context[shortenedTemplateName] = variable;
                partial.Render(result, RenderParameters.FromContext(context, result.FormatProvider));
            });
        }