-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from StuffOfInterest/feature-for
Feature for
- Loading branch information
Showing
6 changed files
with
71 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,5 +28,10 @@ public IActionResult TestWhile() | |
{ | ||
return View(); | ||
} | ||
|
||
public IActionResult TestFor() | ||
{ | ||
return View(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<h2>Test 1</h2> | ||
|
||
@{ int x = default; } | ||
<for initialize="() => x = 0" condition="() => x < 10" update="() => x++"> | ||
<p>Should display @x.</p> | ||
</for> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Razor.TagHelpers; | ||
|
||
namespace LogicTagHelpers | ||
{ | ||
/// <summary> | ||
/// For loop rendering block of code as long as condition is met. | ||
/// </summary> | ||
public class ForTagHelper : TagHelper | ||
{ | ||
/// <summary> | ||
/// Initialize variable for loop control. | ||
/// </summary> | ||
public Action Initialize { get; set; } | ||
|
||
/// <summary> | ||
/// Function to evaluate exit condition. | ||
/// Inner content will render as long as the condition returns true. | ||
/// </summary> | ||
public Func<bool> Condition { get; set; } | ||
|
||
/// <summary> | ||
/// Update to perform at end of each pass through the loop. | ||
/// </summary> | ||
public Action Update { get; set; } | ||
|
||
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) | ||
{ | ||
Initialize?.Invoke(); | ||
|
||
output.TagName = null; | ||
output.Content.Clear(); | ||
|
||
while (Condition?.Invoke() ?? true) | ||
{ | ||
var childContent = await output.GetChildContentAsync(false); | ||
output.Content.AppendHtml(childContent); | ||
|
||
Update?.Invoke(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters