Skip to content

Commit

Permalink
Merge pull request #4 from StuffOfInterest/feature-for
Browse files Browse the repository at this point in the history
Feature for
  • Loading branch information
StuffOfInterest authored Apr 30, 2021
2 parents 9cffd76 + c036edb commit 42dcc8e
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 7 deletions.
5 changes: 5 additions & 0 deletions LogicTagHelpers.Demo/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,10 @@ public IActionResult TestWhile()
{
return View();
}

public IActionResult TestFor()
{
return View();
}
}
}
1 change: 1 addition & 0 deletions LogicTagHelpers.Demo/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
<li><a asp-action="TestSwitch">Test Switch</a></li>
<li><a asp-action="TestForeach">Test Foreach</a></li>
<li><a asp-action="TestWhile">Test While</a></li>
<li><a asp-action="TestFor">Test For</a></li>
</ul>
6 changes: 6 additions & 0 deletions LogicTagHelpers.Demo/Views/Home/TestFor.cshtml
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>
44 changes: 44 additions & 0 deletions LogicTagHelpers/ForTagHelper.cs
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();
}
}
}
}
4 changes: 2 additions & 2 deletions LogicTagHelpers/LogicTagHelpers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.4.0</Version>
<Version>0.5.0</Version>
<Authors>Delbert Matlock</Authors>
<Company>Stuff Of Interest</Company>
<Product />
Expand All @@ -14,9 +14,9 @@ Done:
* switch/case/default
* foreach
* while
* for

Future:
* for
* do</Description>
<PackageProjectUrl>https://github.com/StuffOfInterest/LogicTagHelpers</PackageProjectUrl>
<RepositoryUrl>https://github.com/StuffOfInterest/LogicTagHelpers</RepositoryUrl>
Expand Down
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
* case
* [foreach](#foreach)
* [while](#while)
* [for](#for)

## Future Tags

* for
* do

## Installation
Expand Down Expand Up @@ -56,7 +56,7 @@ Following line must be added to the `_ViewImports.cshtml` file for the logic tag

### foreach

```cshtml
```html
@{ var context = new ForeachContext<(type)>((values-of-type)); }
<foreach iterator="context">
(content to display for each item in collection)
Expand All @@ -65,10 +65,18 @@ Following line must be added to the `_ViewImports.cshtml` file for the logic tag

### while

```cshtml
```html
@{ var x = 0; }
<while condition="() => x < 10">
(content to display while condition is true)
@{ x++; }
(content to display while condition is true)
@{ x++; }
</while>
```

### for
```html
@{ int x = default; }
<for initialize="() => x = 0" condition="() => x < 10" update="() => x++">
(content to display while condition is true)
</for>
```

0 comments on commit 42dcc8e

Please sign in to comment.