Added support for explicit values in equality comparisons in the template, using the _ prefix

This commit is contained in:
Paul Grimshaw 2014-02-05 00:20:33 +00:00
parent a16262d43e
commit b31f393f65
3 changed files with 41 additions and 0 deletions

View File

@ -85,6 +85,11 @@ The **if** tag has complimentary **elif** and **else** tags. There can be as man
The **eq** tag allows you to conditionally include a block of text, by comparing if two values are equal.
{{#eq Name UserName}}Hello {{Name}} !!!{{/eq}}
You can also use specific values as the target value for comparison, rather than values from the model by prefixing the value with the "_" character:
Hello {{#eq User.Role _admin}}Maestro!{{#else}}{{Name}}{{/eq}}
The block will be printed if:
* Both values are null
@ -97,6 +102,9 @@ The **lt** tag allows you to conditionally include a block of text, by comparing
<span {{#lt Budget BudgetLimit}} class="underBudget" {{/lt}}>{{Budget}}</span>
Again, you can use specific values as the target for the comparison parameter, by prefixing the value with the "_" character:
<span {{#lt Budget _500}} class="underBudget" {{/lt}}>{{Budget}}</span>
The block will be printed if:
* Both values are integers or doubles, and the first value is less than the second

View File

@ -1422,5 +1422,35 @@ Odd
}
#endregion
#region ValueIntemplateTests
[TestMethod]
public void TestCompile_CanUseStringValueInEquals()
{
FormatCompiler compiler = new FormatCompiler();
const string format = @"{{#eq Value _Yesterday}}Yes!{{/eq}}";
Generator generator = compiler.Compile(format);
string actual = generator.Render(new {ViewId = "Yesterday"});
string expected = "Yes!";
Assert.AreEqual(expected, actual, "The context variable was not toggled.");
}
[TestMethod]
public void TestCompile_CanUseNumericValueInEquals() {
FormatCompiler compiler = new FormatCompiler();
const string format = @"{{#eq Value _123.3231}}Yes!{{/eq}}";
Generator generator = compiler.Compile(format);
string actual = generator.Render(new { Value = "123.3231" });
string expected = "";
Assert.AreEqual(expected, actual, "The context variable was not toggled.");
}
#endregion
}
}

View File

@ -67,6 +67,9 @@ namespace Mustache
{
value = contextScope.Find(pair.Value.Substring(1));
}
else if (pair.Value.StartsWith("_")) {
value = pair.Value.Remove(0, 1);
}
else
{
value = keyScope.Find(pair.Value);