2013-04-25 01:21:00 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
2013-05-03 12:44:51 +00:00
|
|
|
|
namespace Mustache
|
2013-04-25 01:21:00 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Defines a tag that can iterate over a collection of items and render
|
|
|
|
|
/// the content using each item as the context.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal sealed class EachTagDefinition : ContentTagDefinition
|
|
|
|
|
{
|
|
|
|
|
private const string collectionParameter = "collection";
|
|
|
|
|
private static readonly TagParameter collection = new TagParameter(collectionParameter) { IsRequired = true };
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of an EachTagDefinition.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public EachTagDefinition()
|
|
|
|
|
: base("each", true)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets whether the tag only exists within the scope of its parent.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected override bool GetIsContextSensitive()
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the parameters that can be passed to the tag.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>The parameters.</returns>
|
|
|
|
|
protected override IEnumerable<TagParameter> GetParameters()
|
|
|
|
|
{
|
|
|
|
|
return new TagParameter[] { collection };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the context to use when building the inner text of the tag.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="writer">The text writer passed</param>
|
|
|
|
|
/// <param name="scope">The current scope.</param>
|
|
|
|
|
/// <param name="arguments">The arguments passed to the tag.</param>
|
|
|
|
|
/// <returns>The scope to use when building the inner text of the tag.</returns>
|
|
|
|
|
public override IEnumerable<NestedContext> GetChildContext(TextWriter writer, KeyScope scope, Dictionary<string, object> arguments)
|
|
|
|
|
{
|
|
|
|
|
object value = arguments[collectionParameter];
|
|
|
|
|
IEnumerable enumerable = value as IEnumerable;
|
|
|
|
|
if (enumerable == null)
|
|
|
|
|
{
|
|
|
|
|
yield break;
|
|
|
|
|
}
|
2013-07-20 16:06:38 +00:00
|
|
|
|
int index = 0;
|
2013-04-25 01:21:00 +00:00
|
|
|
|
foreach (object item in enumerable)
|
|
|
|
|
{
|
2013-07-20 16:06:38 +00:00
|
|
|
|
yield return new NestedContext() { KeyScope = scope.CreateChildScope(item), Writer = writer, Data = index };
|
|
|
|
|
++index;
|
2013-04-25 01:21:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the tags that are in scope under this tag.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>The name of the tags that are in scope.</returns>
|
|
|
|
|
protected override IEnumerable<string> GetChildTags()
|
|
|
|
|
{
|
2013-07-20 16:06:38 +00:00
|
|
|
|
return new string[] { "index" };
|
2013-04-25 01:21:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2013-04-25 12:46:03 +00:00
|
|
|
|
/// Gets the parameters that are used to create a new child context.
|
2013-04-25 01:21:00 +00:00
|
|
|
|
/// </summary>
|
2013-04-25 12:46:03 +00:00
|
|
|
|
/// <returns>The parameters that are used to create a new child context.</returns>
|
|
|
|
|
public override IEnumerable<TagParameter> GetChildContextParameters()
|
2013-04-25 01:21:00 +00:00
|
|
|
|
{
|
2013-04-25 12:46:03 +00:00
|
|
|
|
return new TagParameter[] { collection };
|
2013-04-25 01:21:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|