using System; using System.Collections.Generic; using System.Linq; namespace Mustache { /// /// Defines a tag that conditionally prints its content, based on whether the passed in values are equal /// internal sealed class EqTagDefinition : ConditionTagDefinition { private const string ConditionParameter = "condition"; private const string TargetValueParameter = "targetValue"; /// /// Initializes a new instance of a IfTagDefinition. /// public EqTagDefinition() : base("eq") {} /// /// 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[] { new TagParameter(ConditionParameter) { IsRequired = true }, new TagParameter(TargetValueParameter){IsRequired = true} }; } /// /// Gets whether the primary generator group should be used to render the tag. /// /// The arguments passed to the tag. /// /// True if the primary generator group should be used to render the tag; /// otherwise, false to use the secondary group. /// public override bool ShouldGeneratePrimaryGroup(Dictionary arguments) { object condition = arguments[ConditionParameter]; object targetValue = arguments[TargetValueParameter]; return isConditionSatisfied(condition,targetValue); } private bool isConditionSatisfied(object condition,object targetValue) { if (condition == null || targetValue == null) { if (condition == null && targetValue == null) { return true; } return false; } if ((condition is double || condition is int) && (targetValue is double || targetValue is int) ) { return Convert.ToDouble(condition) == Convert.ToDouble(targetValue); } if (condition is string && targetValue is string) { return condition.ToString().Equals(targetValue.ToString(), StringComparison.OrdinalIgnoreCase); } if (condition is bool && targetValue is bool) { return (bool) condition == (bool) targetValue; } if (condition is Char && targetValue is Char) { return (Char)condition == (Char)targetValue; } return false; } /// /// 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[0]; } } }