本文整理汇总了C#中TemplateContext.PushOutput方法的典型用法代码示例。如果您正苦于以下问题:C# TemplateContext.PushOutput方法的具体用法?C# TemplateContext.PushOutput怎么用?C# TemplateContext.PushOutput使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TemplateContext
的用法示例。
在下文中一共展示了TemplateContext.PushOutput方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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);
}
示例2: 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
//.........这里部分代码省略.........