Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for conditionals inside iterators #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions SuperSimpleViewEngine.Tests/SuperSimpleViewEngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,31 @@ public void Should_use_multiple_current_statements_inside_each()
Assert.Equal(@"<html><head></head><body><ul><li id=""Bob"">Bob</li><li id=""Jim"">Jim</li><li id=""Bill"">Bill</li></ul></body></html>", output);
}


[Fact]
public void Should_evaluate_current_conditional_inside_each()
{
const string input = @"<html><head></head><body><ul>@Each.Users;<li>@Current.Name:@If.IsGreekCitizen;<b>Yay Greece!</b>@EndIf;</li>@EndEach;</ul></body></html>";
dynamic model = new ExpandoObject();
model.Users = new List<object>() { new { Name = "Bob", IsGreekCitizen = true }, new { Name = "Malin", IsGreekCitizen = false } };

var output = viewEngine.Render(input, model, this.fakeHost);

Assert.Equal(@"<html><head></head><body><ul><li>Bob:<b>Yay Greece!</b></li><li>Malin:</li></ul></body></html>", output);
}

[Fact]
public void Should_not_evaluate_current_conditional_from_outside_each()
{
const string input = @"<html><head></head><body>@If.HasUsers;Yay Users!@EndIf<ul>@Each.Users;<li>@Current.Name:@If.IsGreekCitizen;<b>Yay Greece!</b>@EndIf;</li>@EndEach;</ul>@IfNot.HasUsers;Yay Users!@EndIf</body></html>";
dynamic model = new ExpandoObject();
model.Users = new List<object>() { new { Name = "Bob", IsGreekCitizen = true }, new { Name = "Malin", IsGreekCitizen = false } };

var output = viewEngine.Render(input, model, this.fakeHost);

Assert.Equal(@"<html><head></head><body>Yay Users!<ul><li>Bob:<b>Yay Greece!</b></li><li>Malin:</li></ul></body></html>", output);
}

[Fact]
public void Should_try_to_use_non_enumerable_in_each_shows_error()
{
Expand Down
5 changes: 4 additions & 1 deletion SuperSimpleViewEngine/SuperSimpleViewEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
});
}
Expand Down