Пример #1
0
        /// <summary>
        /// Initializes dotLiquid's template engine with custom tags.
        /// Unless forceInit==true it only initializes the first time
        /// </summary>
        public static void InitEngine(ITemplateFileSystem fileSystem = null, bool forceInit = false)
        {
            if (initDone && !forceInit)
            {
                return;
            }

            DotLiquidExtensions.RegisterSafeType(typeof(JSchemaDrop));
            DotLiquidExtensions.RegisterSafeType(typeof(KeyValuePair <string, string>));
            DotLiquidExtensions.RegisterSafeType(typeof(KeyValuePair <string, JSchemaDrop>));
            DotLiquidExtensions.RegisterSafeType(typeof(List <JSchemaDrop>));

            Template.NamingConvention = new DotLiquid.NamingConventions.CSharpNamingConvention();

            Condition.Operators["is_empty"] = (left, right) =>
            {
                string asString = left as string ?? right as string;
                if (!string.IsNullOrEmpty(asString))
                {
                    return(false);
                }

                IEnumerable enu = left as IEnumerable ?? right as IEnumerable;
                if (enu != null && enu.OfType <object>().Any())
                {
                    return(false);
                }

                return(true);
            };

            Template.RegisterFilter(typeof(ClassNameFilters));
            Template.RegisterFilter(typeof(CollectionFilters));
            Template.RegisterFilter(typeof(TextFilters));
            Template.RegisterFilter(typeof(ArithmeticFilters));

            Template.RegisterTag <ClassEnumsTag>("ClassEnums");
            Template.RegisterTag <GuidTag>("Guid");
            Template.RegisterTag <MultilineStringEscapeTag>("MultilineStringEscape");
            Template.RegisterTag <FolderTag>("Folder");
            Template.RegisterTag <FileTag>("File");
            Template.RegisterTag <UnzipTag>("Unzip");
            Template.RegisterTag <TrimTag>("Trim");
            Template.RegisterTag <SingleLineTag>("SingleLine");
            Template.RegisterTag <AlignedLines>("AlignLines");
            Template.RegisterTag <TraceTag>("Trace");
            Template.RegisterTag <DebrixTag>("Debrix");


            Template.FileSystem = fileSystem ?? (IFileSystem)(new TemplateFileSystem());

            initDone = true;
        }
Пример #2
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));
            });
        }
Пример #3
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);
        }
Пример #4
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));
            });
        }