diff --git a/SuperSimpleViewEngine.Tests/SuperSimpleViewEngineTests.cs b/SuperSimpleViewEngine.Tests/SuperSimpleViewEngineTests.cs index b291d7d..e0fc036 100644 --- a/SuperSimpleViewEngine.Tests/SuperSimpleViewEngineTests.cs +++ b/SuperSimpleViewEngine.Tests/SuperSimpleViewEngineTests.cs @@ -132,6 +132,31 @@ public void Should_use_multiple_current_statements_inside_each() Assert.Equal(@"", output); } + + [Fact] + public void Should_evaluate_current_conditional_inside_each() + { + const string input = @""; + dynamic model = new ExpandoObject(); + model.Users = new List() { new { Name = "Bob", IsGreekCitizen = true }, new { Name = "Malin", IsGreekCitizen = false } }; + + var output = viewEngine.Render(input, model, this.fakeHost); + + Assert.Equal(@"", output); + } + + [Fact] + public void Should_not_evaluate_current_conditional_from_outside_each() + { + const string input = @"@If.HasUsers;Yay Users!@EndIf@IfNot.HasUsers;Yay Users!@EndIf"; + dynamic model = new ExpandoObject(); + model.Users = new List() { new { Name = "Bob", IsGreekCitizen = true }, new { Name = "Malin", IsGreekCitizen = false } }; + + var output = viewEngine.Render(input, model, this.fakeHost); + + Assert.Equal(@"Yay Users!", output); + } + [Fact] public void Should_try_to_use_non_enumerable_in_each_shows_error() { diff --git a/SuperSimpleViewEngine/SuperSimpleViewEngine.cs b/SuperSimpleViewEngine/SuperSimpleViewEngine.cs index a847582..b8469b4 100644 --- a/SuperSimpleViewEngine/SuperSimpleViewEngine.cs +++ b/SuperSimpleViewEngine/SuperSimpleViewEngine.cs @@ -383,7 +383,9 @@ private string PerformEachSubstitutions(string template, object model, IViewEngi var result = string.Empty; foreach (var item in substitutionEnumerable) { - result += ReplaceCurrentMatch(contents, item, host); + var postConditionalResult = PerformConditionalSubstitutions(contents, item, host); + + result += ReplaceCurrentMatch(postConditionalResult, item, host); } return result; @@ -422,6 +424,7 @@ private string ReplaceCurrentMatch(string contents, object item, IViewEngineHost return string.Empty; } + return eachMatch.Groups["Encode"].Success ? host.HtmlEncode(substitution.Item2.ToString()) : substitution.Item2.ToString(); }); }