From 0ee069f8247fc52b83c9dddcc2f3f3bdb3d6740f Mon Sep 17 00:00:00 2001 From: Andy Boot Date: Mon, 30 Oct 2023 17:08:45 +0000 Subject: [PATCH] Added an `our-tag` tag helper --- Our.Umbraco.TagHelpers/TagTagHelper.cs | 19 ++++++++++++++++ README.md | 31 ++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 Our.Umbraco.TagHelpers/TagTagHelper.cs diff --git a/Our.Umbraco.TagHelpers/TagTagHelper.cs b/Our.Umbraco.TagHelpers/TagTagHelper.cs new file mode 100644 index 0000000..208fa76 --- /dev/null +++ b/Our.Umbraco.TagHelpers/TagTagHelper.cs @@ -0,0 +1,19 @@ +using Microsoft.AspNetCore.Razor.TagHelpers; +using Umbraco.Extensions; + +namespace Our.Umbraco.TagHelpers +{ + [HtmlTargetElement("*", Attributes = "our-tag")] + public class TagTagHelper : TagHelper + { + public string? OurTag { get; set; } + + public override void Process(TagHelperContext context, TagHelperOutput output) + { + if (OurTag?.IsNullOrWhiteSpace() == false) + { + output.TagName = OurTag.ToLower(); + } + } + } +} diff --git a/README.md b/README.md index a0f5850..cadb260 100644 --- a/README.md +++ b/README.md @@ -644,6 +644,37 @@ This will save the file as `/assets/alpinejs.js`, enabling eg. MIME types for .j ### Caching The file is saved once, and never updated unless you manually remove the file. The lookup for the local file is cached in the Runtime Cache. +## `our-tag` +This tag helper attribute can be seamlessly integrated with any DOM element within the Razor file. It allows content editors to oversee SEO by designating the appropriate HTML tags for distinct elements. Consider a scenario where you have an item in your block list named 'Heading Tag' - maybe it's from a predefined dropdown? This is saved as a straightforward string. Your block view then fetches this value through the model, making it accessible for the 'our-tag' tag helper. + +### Example 1 +``` +// Model.Heading = "This is my heading" +// Model.HeadingTag = "H1" +

@Model.Heading

+``` + +#### Output +The hardcoded `h2` has been replaced with an `h1` element tag. +``` +

This is my heading

+``` + +### Example 2 +``` +// Model.Heading = "This is my heading" +// Model.HeadingTag = "" +

@Model.Heading

+``` + +#### Output +Defaults to the original hardcoded element tag: +``` +

This is my heading

+``` + + + ## Video 📺 [![How to create ASP.NET TagHelpers for Umbraco](https://user-images.githubusercontent.com/1389894/138666925-15475216-239f-439d-b989-c67995e5df71.png)](https://www.youtube.com/watch?v=3fkDs0NwIE8)