Esempio n. 1
0
        public async override Task RenderAsync(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 = await templateFileSystem.GetTemplateAsync(context, _templateName).ConfigureAwait(false);
            }
            if (template == null)
            {
                string source = await fileSystem.ReadTemplateFileAsync(context, _templateName).ConfigureAwait(false);

                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();

            await 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)
                    {
                        if (blockState.Parents.TryGetValue(block, out Block 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);
                    }
                }
                return(template.RenderAsync(result, RenderParameters.FromContext(context, result.FormatProvider)));
            }).ConfigureAwait(false);
        }
Esempio n. 2
0
        public async override Task RenderAsync(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 = await templateFileSystem.GetTemplateAsync(context, _templateName).ConfigureAwait(false);
            }
            if (partial == null)
            {
                string source = await fileSystem.ReadTemplateFileAsync(context, _templateName).ConfigureAwait(false);

                partial = Template.Parse(source);
            }

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

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

                if (variable is IEnumerable)
                {
                    foreach (var v in ((IEnumerable)variable).Cast <object>().ToList())
                    {
                        context[shortenedTemplateName] = v;
                        await partial.RenderAsync(result, RenderParameters.FromContext(context, result.FormatProvider)).ConfigureAwait(false);
                    }
                    return;
                }

                context[shortenedTemplateName] = variable;
                await partial.RenderAsync(result, RenderParameters.FromContext(context, result.FormatProvider)).ConfigureAwait(false);
            }).ConfigureAwait(false);
        }