Eq Tag added, to allow values to be compared
This commit is contained in:
parent
599dc7a78f
commit
7c3df01029
|
@ -1,6 +1,6 @@
|
||||||
|
|
||||||
Microsoft Visual Studio Solution File, Format Version 11.00
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
# Visual Studio 2010
|
# Visual Studio 2012
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mustache-sharp", "mustache-sharp\mustache-sharp.csproj", "{D71B378F-A4BA-4263-A4F0-07A49A0C528D}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mustache-sharp", "mustache-sharp\mustache-sharp.csproj", "{D71B378F-A4BA-4263-A4F0-07A49A0C528D}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mustache-sharp.test", "mustache-sharp.test\mustache-sharp.test.csproj", "{7F607362-0680-4751-B1DC-621219294AE3}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mustache-sharp.test", "mustache-sharp.test\mustache-sharp.test.csproj", "{7F607362-0680-4751-B1DC-621219294AE3}"
|
||||||
|
|
|
@ -1158,6 +1158,32 @@ Item Number: foo<br />
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// If the two values don't match, the content of an eq statement should not be printed.
|
||||||
|
/// </summary>
|
||||||
|
[TestMethod]
|
||||||
|
public void TestCompile_Eq_EvaluatesToFalse_SkipsContent() {
|
||||||
|
FormatCompiler parser = new FormatCompiler();
|
||||||
|
const string format = "Before{{#eq OneValue OtherValue}}Content{{/eq}}After";
|
||||||
|
Generator generator = parser.Compile(format);
|
||||||
|
string result = generator.Render(new {OneValue = "Foo", OtherValue = "Bar"} );
|
||||||
|
Assert.AreEqual("BeforeAfter", result, "The wrong text was generated.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// If the two values match, the content of an eq statement should be printed.
|
||||||
|
/// </summary>
|
||||||
|
[TestMethod]
|
||||||
|
public void TestCompile_Eq_EvaluatesToTrue_PrintsContent() {
|
||||||
|
FormatCompiler parser = new FormatCompiler();
|
||||||
|
const string format = "Before{{#eq OneValue OtherValue}}Content{{/eq}}After";
|
||||||
|
Generator generator = parser.Compile(format);
|
||||||
|
string result = generator.Render(new { OneValue = "Foo", OtherValue = "Foo" });
|
||||||
|
Assert.AreEqual("BeforeContentAfter", result, "The wrong text was generated.");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#region Compound Tags
|
#region Compound Tags
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -29,7 +29,7 @@ namespace Mustache
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected override IEnumerable<string> GetClosingTags()
|
protected override IEnumerable<string> GetClosingTags()
|
||||||
{
|
{
|
||||||
return new string[] { "if" };
|
return new string[] { "if","eq" };
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -0,0 +1,89 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace Mustache {
|
||||||
|
/// <summary>
|
||||||
|
/// Defines a tag that conditionally prints its content, based on whether the passed in values are equal
|
||||||
|
/// </summary>
|
||||||
|
internal sealed class EqTagDefinition : ConditionTagDefinition {
|
||||||
|
private const string ConditionParameter = "condition";
|
||||||
|
private const string TargetValueParameter = "targetValue";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of a IfTagDefinition.
|
||||||
|
/// </summary>
|
||||||
|
public EqTagDefinition()
|
||||||
|
: base("eq")
|
||||||
|
{}
|
||||||
|
|
||||||
|
/// <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[] { new TagParameter(ConditionParameter) { IsRequired = true },
|
||||||
|
new TagParameter(TargetValueParameter){IsRequired = true} };
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets whether the primary generator group should be used to render the tag.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="arguments">The arguments passed to the tag.</param>
|
||||||
|
/// <returns>
|
||||||
|
/// True if the primary generator group should be used to render the tag;
|
||||||
|
/// otherwise, false to use the secondary group.
|
||||||
|
/// </returns>
|
||||||
|
public override bool ShouldGeneratePrimaryGroup(Dictionary<string, object> 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 Math.Abs((double) condition - (double) targetValue) < 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the parameters that are used to create a new child context.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>The parameters that are used to create a new child context.</returns>
|
||||||
|
public override IEnumerable<TagParameter> GetChildContextParameters() {
|
||||||
|
return new TagParameter[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -41,6 +41,9 @@ namespace Mustache
|
||||||
_tagLookup.Add(newlineDefinition.Name, newlineDefinition);
|
_tagLookup.Add(newlineDefinition.Name, newlineDefinition);
|
||||||
SetTagDefinition setDefinition = new SetTagDefinition();
|
SetTagDefinition setDefinition = new SetTagDefinition();
|
||||||
_tagLookup.Add(setDefinition.Name, setDefinition);
|
_tagLookup.Add(setDefinition.Name, setDefinition);
|
||||||
|
EqTagDefinition eqTagDefinition = new EqTagDefinition();
|
||||||
|
_tagLookup.Add(eqTagDefinition.Name,eqTagDefinition);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -40,6 +40,7 @@
|
||||||
<Compile Include="ContentTagDefinition.cs" />
|
<Compile Include="ContentTagDefinition.cs" />
|
||||||
<Compile Include="Context.cs" />
|
<Compile Include="Context.cs" />
|
||||||
<Compile Include="ContextParameter.cs" />
|
<Compile Include="ContextParameter.cs" />
|
||||||
|
<Compile Include="EqTagDefinition.cs" />
|
||||||
<Compile Include="VariableFoundEventArgs.cs" />
|
<Compile Include="VariableFoundEventArgs.cs" />
|
||||||
<Compile Include="SetTagDefinition.cs" />
|
<Compile Include="SetTagDefinition.cs" />
|
||||||
<Compile Include="NewlineTagDefinition.cs" />
|
<Compile Include="NewlineTagDefinition.cs" />
|
||||||
|
|
Loading…
Reference in New Issue