Skip to content

Commit

Permalink
Add tag helper to render empty content as a hyphen
Browse files Browse the repository at this point in the history
  • Loading branch information
hortha committed Sep 29, 2023
1 parent 30d7203 commit 9a7b372
Showing 1 changed file with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Mvc.TagHelpers;
using Microsoft.AspNetCore.Razor.TagHelpers;

namespace TeachingRecordSystem.SupportUi.TagHelpers;

[HtmlTargetElement("*", Attributes = "use-empty-fallback")]
public class UseEmptyFallbackTagHelper : TagHelper
{
public override int Order => -1;

public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
var content = await output.GetChildContentAsync();
output.Attributes.RemoveAll("use-empty-fallback");

if (content.IsEmptyOrWhiteSpace)
{
output.AddClass("trs-subtle-emphasis", HtmlEncoder.Default);
output.Content.SetContent("-");
}
}
}

0 comments on commit 9a7b372

Please sign in to comment.