using System;
using System.Collections.Generic;
using System.IO;
namespace mustache
{
///
/// Defines a tag that changes the scope to the object passed as an argument.
///
internal sealed class WithTagDefinition : ContentTagDefinition
{
private const string contextParameter = "context";
private static readonly TagParameter context = new TagParameter(contextParameter) { IsRequired = true };
///
/// Initializes a new instance of a WithTagDefinition.
///
public WithTagDefinition()
: base("with", true)
{
}
///
/// Gets whether the tag only exists within the scope of its parent.
///
protected override bool GetIsContextSensitive()
{
return false;
}
///
/// Gets the parameters that can be passed to the tag.
///
/// The parameters.
protected override IEnumerable GetParameters()
{
return new TagParameter[] { context };
}
///
/// Gets the parameter that is used to create a new child context.
///
/// The parameter that is used to create a new child context.
public override TagParameter GetChildContextParameter()
{
return context;
}
///
/// Gets the context to use when building the inner text of the tag.
///
/// The text writer passed
/// The current scope.
/// The arguments passed to the tag.
/// The scope to use when building the inner text of the tag.
public override IEnumerable GetChildContext(TextWriter writer, KeyScope scope, Dictionary arguments)
{
object context = arguments[contextParameter];
yield return new NestedContext() { KeyScope = scope.CreateChildScope(context), Writer = writer };
}
}
}