本文整理汇总了C#中TemplateContext.SetValue方法的典型用法代码示例。如果您正苦于以下问题:C# TemplateContext.SetValue方法的具体用法?C# TemplateContext.SetValue怎么用?C# TemplateContext.SetValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TemplateContext
的用法示例。
在下文中一共展示了TemplateContext.SetValue方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Evaluate
public override void Evaluate(TemplateContext context)
{
if (Name != null)
{
context.SetValue(Name, this);
}
}
示例2: 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");
}
}
示例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);
}
示例4: Loop
/// <summary>
/// Base implementation for a loop single iteration
/// </summary>
/// <param name="context">The context</param>
/// <param name="index">The index in the loop</param>
/// <returns></returns>
protected bool Loop(TemplateContext context, int index)
{
// Setup variable
context.SetValue(ScriptVariable.LoopFirst, index == 0);
var even = (index & 1) == 0;
context.SetValue(ScriptVariable.LoopEven, even);
context.SetValue(ScriptVariable.LoopOdd, !even);
context.SetValue(ScriptVariable.LoopIndex, index);
Body?.Evaluate(context);
// Return must bubble up to call site
if (context.FlowState == ScriptFlowState.Return)
{
return false;
}
// If we need to break, restore to none state
var result = context.FlowState != ScriptFlowState.Break;
context.FlowState = ScriptFlowState.None;
return result;
}
示例5: 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;
}
示例6: Evaluate
public override void Evaluate(TemplateContext context)
{
var valueObject = context.Evaluate(Value);
context.SetValue(Target, valueObject);
context.Result = valueObject;
}
示例7: Evaluate
public object Evaluate(TemplateContext context, ScriptNode callerContext, ScriptArray parameters, ScriptBlockStatement blockStatement)
{
if (parameters.Count == 0)
{
throw new ScriptRuntimeException(callerContext.Span, "Expecting at least the name of the template to include for the <include> function");
}
string templateName = null;
try
{
templateName = ScriptValueConverter.ToString(callerContext.Span, parameters[0]);
}
catch (Exception ex)
{
throw new ScriptRuntimeException(callerContext.Span, $"Unexpected exception while converting first parameter for <include> function. Expecting a string", ex);
}
// If template name is empty, throw an exception
if (templateName == null || string.IsNullOrEmpty(templateName = templateName.Trim()))
{
throw new ScriptRuntimeException(callerContext.Span, $"Include template name cannot be null or empty");
}
// Compute a new parameters for the include
var newParameters = new ScriptArray(parameters.Count - 1);
for (int i = 1; i < parameters.Count; i++)
{
newParameters[i] = parameters[i];
}
context.SetValue(ScriptVariable.Arguments, newParameters, true);
Template template;
if (!context.CachedTemplates.TryGetValue(templateName, out template))
{
if (context.TemplateLoader == null)
{
throw new ScriptRuntimeException(callerContext.Span,
$"Unable to include <{templateName}>. No TemplateLoader registered in TemplateContext.Options.TemplateLoader");
}
string templateFilePath;
var templateText = context.TemplateLoader.Load(context, callerContext.Span, templateName, out templateFilePath);
if (templateText == null)
{
throw new ScriptRuntimeException(callerContext.Span, $"The result of including <{templateName}> cannot be null");
}
// IF template file path is not defined, we use the template name instead
templateFilePath = templateFilePath ?? templateName;
// Clone parser options
var parserOptions = context.TemplateLoaderParserOptions.Clone();
// Parse include in default modes (while top page can be using front matter)
parserOptions.Mode = parserOptions.Mode == ScriptMode.ScriptOnly
? ScriptMode.ScriptOnly
: ScriptMode.Default;
template = Template.Parse(templateText, templateFilePath, parserOptions);
// If the template has any errors, throw an exception
if (template.HasErrors)
{
throw new ScriptParserRuntimeException(callerContext.Span, $"Error while parsing template <{templateName}> from [{templateFilePath}]", template.Messages);
}
context.CachedTemplates.Add(templateName, template);
}
// Query the pending includes stored in the context
HashSet<string> pendingIncludes;
object pendingIncludesObject;
if (!context.Tags.TryGetValue(typeof(IncludeFunction), out pendingIncludesObject))
{
pendingIncludesObject = pendingIncludes = new HashSet<string>();
context.Tags[typeof (IncludeFunction)] = pendingIncludesObject;
}
else
{
pendingIncludes = (HashSet<string>) pendingIncludesObject;
}
// Make sure that we cannot recursively include a template
if (pendingIncludes.Contains(templateName))
{
throw new ScriptRuntimeException(callerContext.Span, $"The include [{templateName}] cannot be used recursively");
}
pendingIncludes.Add(templateName);
context.PushOutput();
object result = null;
try
{
template.Render(context);
}
finally
//.........这里部分代码省略.........