using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
namespace mustache
{
///
/// Defines a tag that can iterate over a collection of items and render
/// the content using each item as the context.
///
internal sealed class EachTagDefinition : ContentTagDefinition
{
private const string collectionParameter = "collection";
///
/// Initializes a new instance of an EachTagDefinition.
///
public EachTagDefinition()
: base("each", 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[] { new TagParameter(collectionParameter) { IsRequired = true } };
}
///
/// 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 value = arguments[collectionParameter];
IEnumerable enumerable = value as IEnumerable;
if (enumerable == null)
{
yield break;
}
foreach (object item in enumerable)
{
yield return new NestedContext() { KeyScope = scope.CreateChildScope(item), Writer = writer };
}
}
///
/// Gets the tags that are in scope under this tag.
///
/// The name of the tags that are in scope.
protected override IEnumerable GetChildTags()
{
return new string[] { };
}
}
}