当前位置: 首页>>代码示例>>C#>>正文


C# TemplateContext类代码示例

本文整理汇总了C#中TemplateContext的典型用法代码示例。如果您正苦于以下问题:C# TemplateContext类的具体用法?C# TemplateContext怎么用?C# TemplateContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


TemplateContext类属于命名空间,在下文中一共展示了TemplateContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: It_Should_Not_Bog_On_Raw_Text_With_Adaptive_Prediction

        // https://github.com/antlr/antlr4/issues/192#issuecomment-15238595
        public void It_Should_Not_Bog_On_Raw_Text_With_Adaptive_Prediction()
        {
            // Arrange

            DateTime start = DateTime.Now;
            Logger.Log("STARTING...");
            int iterations = 100;

            String template = "<html><body>\r\n";
            for (int i = 0; i < iterations; i++)
            {
                template += "{% for x in (1..10) %} Test {{ x }}{% endfor %}";
                for (int j = 0; j < 100; j++)
                {
                    template += " Raw text "+j;
                }
            }
            template += "</body>\r\n</html>";
            ITemplateContext ctx = new TemplateContext()
                .WithAllFilters();

            // Act

            var result = RenderingHelper.RenderTemplate(template, ctx);

            Logger.Log(result);

            // Assert
            var end = DateTime.Now;
            TimeSpan timeDiff = end - start;
            Logger.Log("ELAPSED: " + timeDiff.TotalMilliseconds);
            Assert.That(timeDiff.Milliseconds < 1000);
            //Assert.Fail("Not Implemented Yet");

        }
开发者ID:gitter-badger,项目名称:Liquid.NET,代码行数:36,代码来源:CapacityTests.cs

示例2: WriteCode

        public override void WriteCode(ISequenceVisitor visitor, TemplateContext ctx)
        {
            //base.WriteCode(ctx);
            var handlerMethod = ctx.CurrentDeclaration.protected_virtual_func(typeof(void), Name, Name.ToLower());
            var handlerInvoke = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), Name);

            foreach (CodeParameterDeclarationExpression item in ctx.CurrentMethod.Parameters)
            {
                handlerMethod.Parameters.Add(item);
                handlerInvoke.Parameters.Add(new CodeVariableReferenceExpression(item.Name));
            }

            //foreach (var item in AllContextVariables)
            //{
            //    if (item.IsSubVariable) continue;
            //    var type = item.SourceVariable == null ? item.Name : item.SourceVariable.RelatedTypeName;
                
                
            //    handlerMethod.Parameters.Add(new CodeParameterDeclarationExpression(
            //       type , item.AsParameter));
            //    handlerInvoke.Parameters.Add(new CodeVariableReferenceExpression(item.ToString()));
            //}
            ctx.CurrentStatements.Add(handlerInvoke);
            //ctx.PushStatements(handlerMethod.Statements);

            //ctx.PopStatements();
        }
开发者ID:InvertGames,项目名称:uFrame.ECS.Editor,代码行数:27,代码来源:UserMethodNode.cs

示例3: Modify

        public override void Modify(object templateInstance, MemberInfo info, TemplateContext ctx)
        {
            base.Modify(templateInstance, info, ctx);
            string strRegex = @"_(?<name>[a-zA-Z0-9]+)_";
            bool replaced = false;
            var newName = Regex.Replace(info.Name, strRegex, _ =>
            {
                var name = _.Groups["name"].Value;
                try
                {
                    replaced = true;
                    return (string) ctx.GetTemplateProperty(templateInstance, name);
                }
                catch (TemplateException ex)
                {
                    return ctx.Item.Name;
                }
                
            });

            if (!replaced && NameFormat != null)
            {
                ctx.CurrentMember.Name = string.Format(NameFormat, ctx.Item.Name.Clean());
            }
            else
            {
                ctx.CurrentMember.Name = newName.Clean();
            }
            
        }
开发者ID:InvertGames,项目名称:uFrame.Editor,代码行数:30,代码来源:GenerateMember.cs

示例4: Eval

        public override void Eval(TemplateContext context, System.IO.StringWriter writer)
        {
            // evaluate test condition
            bool result = EvalCondition(Condition, context);

            if (result)
            {
                foreach (Statement stm in TrueStatements)
                    stm.Eval(context, writer);
                return;
            }
            else
            {
                // process else if statements
                foreach (ElseIfStatement stm in ElseIfStatements)
                {
                    if (EvalCondition(stm.Condition, context))
                    {
                        stm.Eval(context, writer);
                        return;
                    }
                }

                // process else
                foreach (Statement stm in FalseStatements)
                    stm.Eval(context, writer);
            }
        }
开发者ID:jordan49,项目名称:websitepanel,代码行数:28,代码来源:IfStatement.cs

示例5: Evaluate

        public override void Evaluate(TemplateContext context)
        {
            for (int i = 0; i < Statements.Count; i++)
            {
                var statement = Statements[i];

                var expressionStatement = statement as ScriptExpressionStatement;
                var isAssign = expressionStatement?.Expression is ScriptAssignExpression;

                context.Result = null;
                statement.Evaluate(context);

                // Top-level assignment expression don't output anything
                if (isAssign)
                {
                    context.Result = null;
                }
                else if (context.Result != null && context.FlowState != ScriptFlowState.Return && context.EnableOutput)
                {
                    context.Write(Span, context.Result);
                    context.Result = null;
                }

                // If flow state is different, we need to exit this loop
                if (context.FlowState != ScriptFlowState.None)
                {
                    break;
                }
            }
        }
开发者ID:lunet-io,项目名称:scriban,代码行数:30,代码来源:ScriptBlockStatement.cs

示例6: Eval

        public override void Eval(TemplateContext context, System.IO.StringWriter writer)
        {
            // locate template
            if (!context.Templates.ContainsKey(templateName))
                throw new ParserException(String.Format("Custom template \"{0}\" is not defined", templateName), Line, Column);

            TemplateStatement tmp = context.Templates[templateName];

            // create template-specific context
            TemplateContext tmpContext = new TemplateContext();
            tmpContext.ParentContext = context;
            tmpContext.Templates = context.Templates;

            // evaluate inner statements
            StringWriter innerWriter = new StringWriter();
            foreach (Statement stm in Statements)
                stm.Eval(context, innerWriter);
            tmpContext.Variables["innerText"] = innerWriter.ToString();

            // set context variables
            foreach (string name in parameters.Keys)
                tmpContext.Variables[name] = parameters[name].Eval(context);

            // evaluate template statements
            foreach (Statement stm in tmp.Statements)
                stm.Eval(tmpContext, writer);
        }
开发者ID:lwhitelock,项目名称:Websitepanel,代码行数:27,代码来源:CallTemplateStatement.cs

示例7: Parse

        public override Object Parse(Object baseValue, TemplateContext context)
        {
            if (baseValue != null)
            {
                Object[] args = new Object[this.Children.Count];
                Type[] argsType = new Type[this.Children.Count];
                for (Int32 i = 0; i < this.Children.Count; i++)
                {
                    args[i] = this.Children[i].Parse(context);
                    if (args[i] != null)
                    {
                        argsType[i] = args[i].GetType();
                    }
                    else
                    {

                    }
                }


                Object result = Excute(baseValue, context, args, argsType);
                if (result != null)
                {
                    return result;
                }
                result = Common.ReflectionHelpers.GetPropertyValue(baseValue, this.Name);
                if (result != null && result is FuncHandler)
                {
                    return (result as FuncHandler).Invoke(args);
                }
            }

            return null;
        }
开发者ID:webconfig,项目名称:project,代码行数:34,代码来源:FunctaionTag.cs

示例8: ParseCollection

 private void ParseCollection(TagCollection tags, TemplateContext context, System.IO.TextWriter write)
 {
     for (Int32 i = 0; i < tags.Count; i++)
     {
         write.Write( tags[i].Parse(context));
     }
 }
开发者ID:Letractively,项目名称:jntemplate,代码行数:7,代码来源:IfTag.cs

示例9: TheType

 public override string TheType(TemplateContext context)
 {
     var item = context.Item as IMappingsConnectable;
     if (item != null)
     return item.ContextTypeName;
     return base.TheType(context);
 }
开发者ID:InvertGames,项目名称:uFrame.ECS.Templates,代码行数:7,代码来源:SystemTemplate.cs

示例10: It_Should_Allow_Variables_In_Args

        public void It_Should_Allow_Variables_In_Args()
        {
            // Arrange
            const string templateString = "Result : {% tablerow i in array cols: x limit: y offset: z %}{{ i }}{% endtablerow %}";
            TemplateContext ctx = new TemplateContext();
            ctx.DefineLocalVariable("array",
                new ArrayValue(new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 }.Select(x => (IExpressionConstant)NumericValue.Create(x)).ToList())
                );
            ctx.DefineLocalVariable("x", NumericValue.Create(2));
            ctx.DefineLocalVariable("y", NumericValue.Create(3));
            ctx.DefineLocalVariable("z", NumericValue.Create(1));
            var template = LiquidTemplate.Create(templateString);

            // Act
            String result = template.Render(ctx);
            Logger.Log(result);

            // Assert
            Assert.That(result, Is.StringContaining("<tr class=\"row1\">"));
            Assert.That(result, Is.StringContaining("<tr class=\"row2\">"));
            Assert.That(result, Is.Not.StringContaining("<tr class=\"row3\">"));
            Assert.That(result, Is.Not.StringContaining(">1</td>"));
            Assert.That(result, Is.StringContaining(">2</td>"));
            Assert.That(result, Is.StringContaining(">4</td>"));
            Assert.That(result, Is.Not.StringContaining(">5</td>"));
            
        }
开发者ID:gitter-badger,项目名称:Liquid.NET,代码行数:27,代码来源:TableRowBlockTagTests.cs

示例11: Excute

        private void Excute(TemplateContext context, StringBuilder writer)
        {
            this.Initial.Parse(context);
            //如果标签为空,则直接为false,避免死循环以内存溢出
            Boolean run;

            if (this.Test == null)
            {
                run = false;
            }
            else
            {
                run = this.Test.ToBoolean(context);
            }

            while (run)
            {
                for (Int32 i = 0; i < this.Children.Count; i++)
                {
                    this.Children[i].Parse(context, writer);
                }
                if (this.Do != null)
                {
                    this.Do.Parse(context);
                }
                run = this.Test == null ? true : this.Test.ToBoolean(context);
            }
        }
开发者ID:webconfig,项目名称:project,代码行数:28,代码来源:ForTag.cs

示例12: Evaluate

        public override void Evaluate(TemplateContext context)
        {
            switch (Operator)
            {
                case ScriptUnaryOperator.Not:
                {
                    var value = context.Evaluate(Right);
                    context.Result = !ScriptValueConverter.ToBool(value);
                }
                    break;
                case ScriptUnaryOperator.Negate:
                case ScriptUnaryOperator.Plus:
                {
                    var value = context.Evaluate(Right);

                    bool negate = Operator == ScriptUnaryOperator.Negate;

                    var customType = value as IScriptCustomType;
                    if (customType != null)
                    {
                        context.Result = customType.EvaluateUnaryExpression(this);
                    }
                    else if (value != null)
                    {
                        if (value is int)
                        {
                            context.Result = negate ? -((int) value) : value;
                        }
                        else if (value is double)
                        {
                            context.Result = negate ? -((double) value) : value;
                        }
                        else if (value is float)
                        {
                            context.Result = negate ? -((float) value) : value;
                        }
                        else if (value is long)
                        {
                            context.Result = negate ? -((long) value) : value;
                        }
                        else
                        {
                            throw new ScriptRuntimeException(this.Span,
                                $"Unexpected value [{value} / Type: {value?.GetType()}]. Cannot negate(-)/positive(+) a non-numeric value");
                        }
                    }
                }
                    break;
                case ScriptUnaryOperator.FunctionAlias:
                    context.Result = context.Evaluate(Right, true);
                    break;

                case ScriptUnaryOperator.FunctionParametersExpand:
                    // Function parameters expand is done at the function level, so here, we simply return the actual list
                    Right?.Evaluate(context);
                    break;
                default:
                    throw new ScriptRuntimeException(Span, $"Operator [{Operator}] is not supported");
            }
        }
开发者ID:lunet-io,项目名称:scriban,代码行数:60,代码来源:ScriptUnaryExpression.cs

示例13: TestFrontMatter

        public void TestFrontMatter()
        {
            var options = new ParserOptions() {Mode = ScriptMode.FrontMatter};
            var template = ParseTemplate(@"{{ 
variable = 1
name = 'yes'
}}
This is after the frontmatter: {{ name }}
{{
variable + 1
}}", options);

            // Make sure that we have a front matter
            Assert.NotNull(template.Page.FrontMatter);

            var context = new TemplateContext();

            // Evaluate front-matter
            var frontResult = context.Evaluate(template.Page.FrontMatter);
            Assert.Null(frontResult);
            
            // Evaluate page-content
            context.Evaluate(template.Page);
            var pageResult = context.Output.ToString();
            Assert.AreEqual(@"This is after the frontmatter: yes
2", pageResult);
        }
开发者ID:lunet-io,项目名称:scriban,代码行数:27,代码来源:TestParser.cs

示例14: It_Should_Dereference_A_Nested_Dictionary

        public void It_Should_Dereference_A_Nested_Dictionary()
        {

            // Arrange
            var dict3 = new Dictionary<String, IExpressionConstant>
            {
                {"str", new StringValue("Dict 3")},
            };
            DictionaryValue dictValue3 = new DictionaryValue(dict3);

            var dict2 = new Dictionary<String, IExpressionConstant>
            {
                {"dict3", dictValue3}
            };
            DictionaryValue dictValue2 = new DictionaryValue(dict2);

            var dict = new Dictionary<String, IExpressionConstant>
            {
                {"dict2", dictValue2}        
            };
            DictionaryValue dictValue = new DictionaryValue(dict);

            ITemplateContext ctx = new TemplateContext()
                .DefineLocalVariable("dict1", dictValue);
            
            // Act
            var result = RenderingHelper.RenderTemplate("Result : {{dict1.dict2.dict3.str}}->{% if dict1.dict2.dict3.str == \"Dict 2\" %}Dict2{% elseif dict1.dict2.dict3.str == \"Dict 3\" %}Dict3{%endif%}", ctx);

            // Assert
            Assert.That(result, Is.EqualTo("Result : Dict 3->Dict3"));
        }
开发者ID:gitter-badger,项目名称:Liquid.NET,代码行数:31,代码来源:DictionaryValueTests.cs

示例15: CreateWindowCore

 protected override Window CreateWindowCore(TemplateContext context, ICollection<Controller> controllers, bool isMain, bool activateControllersImmediatelly) {
     Tracing.Tracer.LogVerboseValue("WinApplication.CreateWindowCore.activateControllersImmediatelly", activateControllersImmediatelly);
     var windowCreatingEventArgs = new WindowCreatingEventArgs();
     OnWindowCreating(windowCreatingEventArgs);
     return windowCreatingEventArgs.Handled? windowCreatingEventArgs.Window
                : new XpandWebWindow(this, context, controllers, isMain, activateControllersImmediatelly);
 }
开发者ID:kamchung322,项目名称:eXpand,代码行数:7,代码来源:XpandWebApplication.cs


注:本文中的TemplateContext类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。