using System;
using System.Collections;
using System.Collections.Generic;
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 scopes for each of the items found in the argument.
///
/// The current scope.
/// The arguments passed to the tag.
/// The scopes for each of the items found in the argument.
public override IEnumerable GetChildScopes(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 scope.CreateChildScope(item);
}
}
///
/// 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[] { };
}
}
}