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


C# TemplateContext.Evaluate方法代码示例

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


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

示例1: 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

示例2: 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

示例3: Evaluate

        public override void Evaluate(TemplateContext context)
        {
            // unit test: 230-capture-statement.txt
            context.PushOutput();
            {
                context.Evaluate(Body);
            }
            var result = context.PopOutput();

            context.SetValue(Target, result);
        }
开发者ID:lunet-io,项目名称:scriban,代码行数:11,代码来源:ScriptCaptureStatement.cs

示例4: Evaluate

 public override void Evaluate(TemplateContext context)
 {
     var conditionValue = ScriptValueConverter.ToBool(context.Evaluate(Condition));
     if (conditionValue)
     {
         Then?.Evaluate(context);
     }
     else
     {
         base.Evaluate(context);
     }
 }
开发者ID:lunet-io,项目名称:scriban,代码行数:12,代码来源:ScriptIfStatement.cs

示例5: Evaluate

        public override void Evaluate(TemplateContext context)
        {
            // Call evaluate on the target, but don't automatically call the function as if it was a parameterless call.
            var targetFunction = context.Evaluate(Target, true);

            // Throw an exception if the target function is null
            if (targetFunction == null)
            {
                throw new ScriptRuntimeException(Target.Span, $"The target function [{Target}] is null");
            }

            context.Result = Call(context, this, targetFunction, Arguments);
        }
开发者ID:lunet-io,项目名称:scriban,代码行数:13,代码来源:ScriptFunctionCall.cs

示例6: Evaluate

        public override void Evaluate(TemplateContext context)
        {
            // Check that the Target is actually a function
            var functionCall = Target as ScriptFunctionCall;
            if (functionCall == null)
            {
                var parameterLessFunction = context.Evaluate(Target, true);
                if (!(parameterLessFunction is IScriptCustomFunction))
                {
                    var targetPrettyname = ScriptSyntaxAttribute.Get(Target);
                    throw new ScriptRuntimeException(Target.Span, $"Expecting a direct function instead of the expression [{Target}/{targetPrettyname.Name}]");
                }

                context.BlockDelegates.Push(Body);
                context.Result = ScriptFunctionCall.Call(context, this, parameterLessFunction);
            }
            else
            {
                context.BlockDelegates.Push(Body);
                context.Result = context.Evaluate(functionCall);
            }
        }
开发者ID:lunet-io,项目名称:scriban,代码行数:22,代码来源:ScriptWrapStatement.cs

示例7: Evaluate

        public override void Evaluate(TemplateContext context)
        {
            var scriptObject = new ScriptObject();
            foreach (var member in Members)
            {
                var variable = member.Key as ScriptVariable;
                var literal = member.Key as ScriptLiteral;

                var name = variable?.Name ?? literal?.Value?.ToString();
                scriptObject.SetValue(name, context.Evaluate(member.Value), false);
            }
            context.Result = scriptObject;
        }
开发者ID:lunet-io,项目名称:scriban,代码行数:13,代码来源:ScriptObjectInitializerExpression.cs

示例8: EvaluateImpl

        protected override void EvaluateImpl(TemplateContext context)
        {
            var loopIterator = context.Evaluate(Iterator);
            var iterator = loopIterator as IEnumerable;
            if (iterator != null)
            {
                var index = -1;
                object previousValue = null;
                bool escape = false;

                foreach (var value in iterator)
                {
                    if (!context.StepLoop())
                    {
                        return;
                    }

                    // We update on next run on previous value (in order to handle last)
                    if (index >= 0)
                    {
                        context.SetValue(ScriptVariable.LoopLast, false);
                        context.SetValue(Variable, previousValue);

                        if (!Loop(context, index))
                        {
                            escape = true;
                            break;
                        }
                    }

                    previousValue = value;
                    index++;
                }

                if (!escape && index >= 0 && context.StepLoop())
                {
                    context.SetValue(ScriptVariable.LoopLast, true);
                    context.SetValue(Variable, previousValue);
                    Loop(context, index);
                }
            }
            else if (loopIterator != null)
            {
                throw new ScriptRuntimeException(Iterator.Span, $"Unexpected type [{loopIterator.GetType()}] for iterator");
            }
        }
开发者ID:lunet-io,项目名称:scriban,代码行数:46,代码来源:ScriptForStatement.cs

示例9: EvaluateImpl

        protected override void EvaluateImpl(TemplateContext context)
        {
            var index = 0;
            while(context.StepLoop())
            {
                var conditionResult = ScriptValueConverter.ToBool(context.Evaluate(Condition));
                if (!conditionResult)
                {
                    break;
                }

                if (!Loop(context, index++))
                {
                    break;
                }
            };
        }
开发者ID:lunet-io,项目名称:scriban,代码行数:17,代码来源:ScriptWhileStatement.cs

示例10: Evaluate

        public override void Evaluate(TemplateContext context)
        {
            var target = context.GetValue(Name);
            if (!(target is IScriptObject))
            {
                var targetName = target?.GetType().Name ?? "null";
                throw new ScriptRuntimeException(Name.Span, $"Invalid target property [{Name}] used for [with] statement. Must be a ScriptObject instead of [{targetName}]");
            }

            context.PushGlobal((IScriptObject)target);
            try
            {
                context.Evaluate(Body);
            }
            finally
            {
                context.PopGlobal();
            }
        }
开发者ID:lunet-io,项目名称:scriban,代码行数:19,代码来源:ScriptWithStatement.cs

示例11: Call

        public static object Call(TemplateContext context, ScriptNode callerContext, object functionObject, List<ScriptExpression> arguments = null)
        {
            if (callerContext == null) throw new ArgumentNullException(nameof(callerContext));
            if (functionObject == null)
            {
                throw new ScriptRuntimeException(callerContext.Span, $"The target function [{callerContext}] is null");
            }
            var function = functionObject as ScriptFunction;
            var externFunction = functionObject as IScriptCustomFunction;

            if (function == null && externFunction == null)
            {
                throw new ScriptRuntimeException(callerContext.Span, $"Invalid object function [{functionObject?.GetType()}]");
            }

            ScriptBlockStatement blockDelegate = null;
            if (context.BlockDelegates.Count > 0)
            {
                blockDelegate = context.BlockDelegates.Pop();
            }

            var argumentValues = new ScriptArray();
            if (arguments != null)
            {
                foreach (var argument in arguments)
                {
                    var value = context.Evaluate(argument);

                    // Handle parameters expansion for a function call when the operator ^ is used
                    var unaryExpression = argument as ScriptUnaryExpression;
                    if (unaryExpression != null && unaryExpression.ExpandParameters(value, argumentValues))
                    {
                        continue;
                    }

                    argumentValues.Add(value);
                }
            }

            // Handle pipe arguments here
            if (context.PipeArguments.Count > 0)
            {
                var additionalArgument = context.PipeArguments.Pop();

                var value = context.Evaluate(additionalArgument);

                // Handle parameters expansion for a function call when the operator ~ is used
                var unaryExpression = additionalArgument as ScriptUnaryExpression;
                if (unaryExpression == null || !unaryExpression.ExpandParameters(value, argumentValues))
                {
                    argumentValues.Add(value);
                }
            }

            object result = null;
            context.EnterFunction(callerContext);
            try
            {
                if (externFunction != null)
                {
                    result = externFunction.Evaluate(context, callerContext, argumentValues, blockDelegate);
                }
                else
                {
                    context.SetValue(ScriptVariable.Arguments, argumentValues, true);

                    // Set the block delegate
                    if (blockDelegate != null)
                    {
                        context.SetValue(ScriptVariable.BlockDelegate, blockDelegate, true);
                    }
                    result = context.Evaluate(function.Body);
                }
            }
            finally
            {
                context.ExitFunction();
            }

            // Restore the flow state to none
            context.FlowState = ScriptFlowState.None;
            return result;
        }
开发者ID:lunet-io,项目名称:scriban,代码行数:83,代码来源:ScriptFunctionCall.cs

示例12: Evaluate

 public override void Evaluate(TemplateContext context)
 {
     var valueObject = context.Evaluate(Value);
     context.SetValue(Target, valueObject);
     context.Result = valueObject;
 }
开发者ID:lunet-io,项目名称:scriban,代码行数:6,代码来源:ScriptAssignExpression.cs

示例13: Evaluate

        public override void Evaluate(TemplateContext context)
        {
            var leftValueOriginal = context.Evaluate(Left);
            var leftValue = leftValueOriginal;
            var rightValueOriginal = context.Evaluate(Right);
            object rightValue = rightValueOriginal;

            if (Operator == ScriptBinaryOperator.EmptyCoalescing)
            {
                context.Result = leftValue ?? rightValue;
                return;
            }
            else if (Operator == ScriptBinaryOperator.And || Operator == ScriptBinaryOperator.Or)
            {
                var leftBoolValue = ScriptValueConverter.ToBool(leftValue);
                var rightBoolValue = ScriptValueConverter.ToBool(rightValue);
                if (Operator == ScriptBinaryOperator.And)
                {
                    context.Result = leftBoolValue && rightBoolValue;
                }
                else
                {
                    context.Result = leftBoolValue || rightBoolValue;
                }

                return;
            }
            else
            {
                switch (Operator)
                {
                    case ScriptBinaryOperator.ShiftLeft:
                    case ScriptBinaryOperator.ShiftRight:
                        if (leftValue is IList || rightValue is IList)
                        {
                            // Special path for IList to allow custom binary expression
                            context.Result = ScriptArray.CustomOperator.EvaluateBinaryExpression(this, leftValue,
                                rightValue);
                            return;
                        }
                        break;
                    case ScriptBinaryOperator.CompareEqual:
                    case ScriptBinaryOperator.CompareNotEqual:
                    case ScriptBinaryOperator.CompareGreater:
                    case ScriptBinaryOperator.CompareLess:
                    case ScriptBinaryOperator.CompareGreaterOrEqual:
                    case ScriptBinaryOperator.CompareLessOrEqual:
                    case ScriptBinaryOperator.Add:
                    case ScriptBinaryOperator.Substract:
                    case ScriptBinaryOperator.Multiply:
                    case ScriptBinaryOperator.Divide:
                    case ScriptBinaryOperator.DivideRound:
                    case ScriptBinaryOperator.Modulus:
                    case ScriptBinaryOperator.RangeInclude:
                    case ScriptBinaryOperator.RangeExclude:
                        var leftType = leftValue?.GetType();
                        var rightType = rightValue?.GetType();

                        if (leftValue is string || rightValue is string)
                        {
                            context.Result = CalculateToString(Operator, leftValue, rightValue);
                            {
                                // TODO: Log an error if CalculateToString return null?
                                //context.LogError(Span, $"Operation [{Operator}] on strings not supported");
                            }
                        }
                        else
                        {
                            context.Result = Calculate(Operator, leftValue, leftType, rightValue, rightType);
                        }
                        return;
                }
            }

            throw new ScriptRuntimeException(Span, $"Operator [{Operator.ToText()}] is not implemented for the left [{Left}] / right [{Right}]");
        }
开发者ID:lunet-io,项目名称:scriban,代码行数:76,代码来源:ScriptBinaryExpression.cs


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