Skip to content

Commit

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

public IActionResult TestDo()
{
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 @@ -8,4 +8,5 @@
<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>
<li><a asp-action="TestDo">Test Do</a></li>
</ul>
20 changes: 20 additions & 0 deletions LogicTagHelpers.Demo/Views/Home/TestDo.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<h2>Test 1</h2>

@{ var x = 0; }
<do condition="() => x < 10">
@{ x++; }
<p>Should display @x.</p>
</do>

<h2>Test 2</h2>

@try
{
<do>
<p>Should not display.</p>
</do>
}
catch (Exception ex)
{
<p>Expected exception occurred: '@ex.Message'.</p>
}
15 changes: 14 additions & 1 deletion LogicTagHelpers.Demo/Views/Home/TestFor.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,17 @@
@{ int x = default; }
<for initialize="() => x = 0" condition="() => x < 10" update="() => x++">
<p>Should display @x.</p>
</for>
</for>

<h2>Test 2</h2>

@try
{
<for>
<p>Should not display.</p>
</for>
}
catch (Exception ex)
{
<p>Expected exception occurred: '@ex.Message'.</p>
}
20 changes: 20 additions & 0 deletions LogicTagHelpers.Tests/CreateClassTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,25 @@ public void CanCreateWhileTagHelper()
// Assert
Assert.IsNotNull(result);
}

[TestMethod]
public void CanCreateForTagHelper()
{
// Act
var result = new ForTagHelper();

// Assert
Assert.IsNotNull(result);
}

[TestMethod]
public void CanCreateDoTagHelper()
{
// Act
var result = new DoTagHelper();

// Assert
Assert.IsNotNull(result);
}
}
}
35 changes: 35 additions & 0 deletions LogicTagHelpers/DoTagHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.TagHelpers;

namespace LogicTagHelpers
{
/// <summary>
/// Do loop render block of code until condition is no longer met.
/// </summary>
public class DoTagHelper : TagHelper
{
/// <summary>
/// Function to evaluate exit condition.
/// Inner content will render as long as the condition returns true.
/// </summary>
public Func<bool> Condition { get; set; }

public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
if (Condition == null)
{
throw new LogicTagHelperException("Condition may not be null.");
}

output.TagName = null;
output.Content.Clear();

do
{
var childContent = await output.GetChildContentAsync(false);
output.Content.AppendHtml(childContent);
} while (Condition.Invoke());
}
}
}
7 changes: 6 additions & 1 deletion LogicTagHelpers/ForTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@ public class ForTagHelper : TagHelper

public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
if (Condition == null)
{
throw new LogicTagHelperException("Condition may not be null.");
}

Initialize?.Invoke();

output.TagName = null;
output.Content.Clear();

while (Condition?.Invoke() ?? true)
while (Condition.Invoke())
{
var childContent = await output.GetChildContentAsync(false);
output.Content.AppendHtml(childContent);
Expand Down
11 changes: 4 additions & 7 deletions LogicTagHelpers/LogicTagHelpers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,18 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.5.0</Version>
<Version>0.6.0</Version>
<Authors>Delbert Matlock</Authors>
<Company>Stuff Of Interest</Company>
<Product />
<Description>Tag helpers to provide control logic inside of cshtml page.
<Description>Tag helpers to provide control logic inside of cshtml pages.

Done:
* if/then/else
* switch/case/default
* foreach
* while
* do (also known as 'do-while')
* for

Future:
* do</Description>
* foreach</Description>
<PackageProjectUrl>https://github.com/StuffOfInterest/LogicTagHelpers</PackageProjectUrl>
<RepositoryUrl>https://github.com/StuffOfInterest/LogicTagHelpers</RepositoryUrl>
<LangVersion>8</LangVersion>
Expand Down
51 changes: 30 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
# Logic Tag Helpers

Tag helpers to provide control logic inside of cshtml pages.

| CI Build | Publish Build |
| -------- | ------------- |
| [![.NET](https://github.com/StuffOfInterest/LogicTagHelpers/actions/workflows/build-test.yml/badge.svg)](https://github.com/StuffOfInterest/LogicTagHelpers/actions/workflows/build-test.yml) | [![.NET](https://github.com/StuffOfInterest/LogicTagHelpers/actions/workflows/build-test-publish.yml/badge.svg)](https://github.com/StuffOfInterest/LogicTagHelpers/actions/workflows/build-test-publish.yml) |

## Completed Tags
## Tags

* [if](#if)
* then
* else
* [switch](#switch)
* case
* [foreach](#foreach)
* [while](#while)
* [do](#do)
* [for](#for)

## Future Tags

* do
* [foreach](#foreach)

## Installation

Expand All @@ -27,56 +26,66 @@ Following line must be added to the `_ViewImports.cshtml` file for the logic tag
@addTagHelper *, LogicTagHelpers
```

## Tags
## Tag Examples

### if

```html
<if condition="(boolean)">
<then>(content to display if condition matched)</then>
<else>(content to display if condition not matched)</else>
<then><span>content to display if condition matched</span></then>
<else><span>content to display if condition not matched</span></else>
</if>
```

```html
<if condition="(boolean)" direct="true">
(content to display if condition matched)
<span>content to display if condition matched</span>
</if>
```

### switch

```html
<switch expression="(variable)">
<case value="(value)">(content to display on value match)</case>
<case value="(value)">(content to display on value match)</case>
<default>(content to display if no value match)</default>
<case value="(value)"><span>content to display on value match</span></case>
<case value="(value)"><span>content to display on value match</span></case>
<default><span>content to display if no value match</span></default>
</switch>
```

### foreach
### while

```html
@{ var context = new ForeachContext<(type)>((values-of-type)); }
<foreach iterator="context">
(content to display for each item in collection)
</foreach>
@{ var x = 0; }
<while condition="() => x < 10">
<span>content to display while condition is true</span>
@{ x++; }
</while>
```

### while
### do

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

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

### foreach

```html
@{ var context = new ForeachContext<(type)>((values-of-type)); }
<foreach iterator="context">
<span>content to display for each item in collection</span>
</foreach>
```

0 comments on commit 124199d

Please sign in to comment.