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 parameters that are used to create a new child context.
///
/// The parameters that are used to create a new child context.
public override IEnumerable GetChildContextParameters()
{
return new TagParameter[] { context };
}
///
/// Gets the context to use when building the inner text of the tag.
///
/// The text writer passed
/// The current key 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,
Scope keyScope,
Dictionary arguments,
Scope contextScope)
{
object contextSource = arguments[contextParameter];
NestedContext context = new NestedContext()
{
KeyScope = keyScope.CreateChildScope(contextSource),
Writer = writer,
ContextScope = contextScope.CreateChildScope()
};
yield return context;
}
}
}