Files
TurnstileTag/TurnstileAttributeTagHelper.cs
T
2024-04-05 13:03:12 +02:00

43 lines
1.0 KiB
C#

using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
namespace TurnstileTag;
[HtmlTargetElement("form", Attributes = "cf-turnstile")]
public class TurnstileAttributeTagHelper : TagHelper
{
readonly ITurnstile _turnstile;
public TurnstileAttributeTagHelper(ITurnstile turnstile)
{
_turnstile = turnstile;
}
/// <summary>
/// Whether turnstile should be activated.
/// </summary>
public bool? CfTurnstile { get; set; }
/// <summary>
/// Gets the <see cref="Rendering.ViewContext"/> of the executing view.
/// </summary>
[HtmlAttributeNotBound]
[ViewContext]
public ViewContext? ViewContext { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
if (CfTurnstile ?? false)
{
IHtmlContent content = _turnstile.GenerateTurnstileFormHtml(ViewContext!);
if (content != null)
{
output.PostContent.AppendHtml(content);
}
}
}
}