Added support for explicit values in equality comparisons in the template, using the _ prefix
This commit is contained in:
parent
a16262d43e
commit
b31f393f65
|
@ -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.
|
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}}
|
{{#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:
|
The block will be printed if:
|
||||||
* Both values are null
|
* 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>
|
<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:
|
The block will be printed if:
|
||||||
* Both values are integers or doubles, and the first value is less than the second
|
* Both values are integers or doubles, and the first value is less than the second
|
||||||
|
|
|
@ -1422,5 +1422,35 @@ Odd
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#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
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,6 +67,9 @@ namespace Mustache
|
||||||
{
|
{
|
||||||
value = contextScope.Find(pair.Value.Substring(1));
|
value = contextScope.Find(pair.Value.Substring(1));
|
||||||
}
|
}
|
||||||
|
else if (pair.Value.StartsWith("_")) {
|
||||||
|
value = pair.Value.Remove(0, 1);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
value = keyScope.Find(pair.Value);
|
value = keyScope.Find(pair.Value);
|
||||||
|
|
Loading…
Reference in New Issue