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 : TagDefinition { private const string collectionParameter = "collection"; /// /// Initializes a new instance of an EachTagDefinition. /// public EachTagDefinition() : base("each", true) { } /// /// Gets the parameters that can be passed to the tag. /// /// The parameters. protected override TagParameter[] GetParameters() { return new TagParameter[] { new TagParameter(collectionParameter) { IsRequired = true } }; } /// /// Gets whether the tag has content. /// public override bool HasBody { get { return true; } } /// /// Gets the tags that come into scope within the context of the tag. /// /// The tag definitions. protected override TagDefinition[] GetChildTags() { return new TagDefinition[0]; } /// /// 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); } } } }