Skip to content

Commit 1630b78

Browse files
author
Warren Buckley
authored
Merge pull request #35 from umbraco-community/feature/matt-wise-linktaghelper
Adds in Matt Wise work submitted as an issues as opposed to a PR in #32
2 parents e18c434 + d30dfb1 commit 1630b78

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using Microsoft.AspNetCore.Razor.TagHelpers;
2+
using System.Threading.Tasks;
3+
using Umbraco.Cms.Core.Models;
4+
5+
namespace Our.Umbraco.TagHelpers
6+
{
7+
[HtmlTargetElement("our-link")]
8+
public class LinkTagHelper : TagHelper
9+
{
10+
[HtmlAttributeName("Link")]
11+
public Link? Link { get; set; }
12+
13+
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
14+
{
15+
// Ensure we have a Url set on the LinkPicker property editor in Umbraco
16+
if (string.IsNullOrWhiteSpace(Link?.Url))
17+
{
18+
output.SuppressOutput();
19+
return;
20+
}
21+
22+
// If the <our-link /> is self closing
23+
// Ensure that our <a></a> always has a matching end tag
24+
output.TagMode = TagMode.StartTagAndEndTag;
25+
26+
output.TagName = "a";
27+
var childContent = await output.GetChildContentAsync();
28+
29+
// If we use the TagHelper <umb-link></umb-link>
30+
// Without child DOM elements then it will use the Link Name property inside the <a> it generates
31+
if (childContent.IsEmptyOrWhiteSpace)
32+
{
33+
output.Content.SetContent(Link.Name);
34+
}
35+
36+
// Set the HREF of the <a>
37+
output.Attributes.SetAttribute("href", Link.Url);
38+
39+
if (string.IsNullOrWhiteSpace(Link.Target))
40+
{
41+
return;
42+
}
43+
44+
// Set the <a target=""> attribute such as _blank etc...
45+
output.Attributes.SetAttribute("target", Link.Target);
46+
47+
// If the target is _blank & not an internal picked content node & external
48+
// Ensure we set the <a rel="noopener"> attribute
49+
if (Link.Target == "_blank" && Link.Type == LinkType.External)
50+
{
51+
output.Attributes.SetAttribute("rel", "noopener");
52+
}
53+
}
54+
}
55+
}

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,25 @@ Alternatively you can use the `our-active-class` attribute in conjuction with `o
319319
</ul>
320320
```
321321

322+
## `<our-link>`
323+
This tag helper element `<our-link>` will create a simple anchor tag using the Umbraco Multi Url Picker property editor that uses the C# Model `Umbraco.Cms.Core.Models.Link`
324+
325+
Note if the link set is an external link and you set the target of the link to be `_blank`, then the link will have the noopener attribute added to the link.
326+
327+
### Simple Example
328+
```cshtml
329+
<our-link="@Model.ctaLink">
330+
<h2>Hi There</h2>
331+
</our-link>
332+
```
333+
334+
Alternatively if you use the `<our-link>` without child DOM elements then it will use the `Title` property of the link in the Multi Url Picker property editor to create the anchor tag.
335+
336+
```cshtml
337+
<our-link="@Model.ctaLink"></our-link>
338+
```
339+
340+
With this tag helper the child DOM elements inside the `<our-link>` is wrapped with the `<a>` tag
322341

323342
## Video 📺
324343
[![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)

0 commit comments

Comments
 (0)