Skip to content

Commit 5803bf4

Browse files
author
Warren Buckley
authored
Merge pull request #17 from umbraco-community/feature/our-fallback
Adds <our-fallback>
2 parents f2c19af + 91af526 commit 5803bf4

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using Microsoft.AspNetCore.Mvc.Rendering;
2+
using Microsoft.AspNetCore.Mvc.ViewFeatures;
3+
using Microsoft.AspNetCore.Razor.TagHelpers;
4+
using Umbraco.Cms.Core.Models.PublishedContent;
5+
using Umbraco.Extensions;
6+
7+
namespace Our.Umbraco.TagHelpers
8+
{
9+
/// <summary>
10+
/// Fallback Umbraco properties
11+
/// </summary>
12+
[HtmlTargetElement("our-fallback")]
13+
public class OurFallbackTagHelper : TagHelper
14+
{
15+
/// <summary>
16+
/// The property on the Model to use as a fallback such as Model.Header
17+
/// Would just be Header
18+
/// </summary>
19+
[HtmlAttributeName("property")]
20+
public ModelExpression ModelProperty { get; set; }
21+
22+
[HtmlAttributeName("mode")]
23+
public Fallback Mode { get; set; }
24+
25+
/// <summary>
26+
/// If using the fallback mode as Language then you can specify the culture to fallback to
27+
/// </summary>
28+
[HtmlAttributeName("culture")]
29+
public string CultureCode { get; set; } = null;
30+
31+
[HtmlAttributeNotBound]
32+
[ViewContext]
33+
public ViewContext ViewContext { get; set; }
34+
35+
public override void Process(TagHelperContext context, TagHelperOutput output)
36+
{
37+
// Check to see that the Model Property is NOT a complex type or a collection of things
38+
var isComplexType = ModelProperty.ModelExplorer.Metadata.IsComplexType;
39+
var isEnumerableType = ModelProperty.ModelExplorer.Metadata.IsEnumerableType;
40+
41+
// Only can support simpler things such as strings (as if collection)
42+
// We have no way of knowing how they want to iterate and display that HTML etc
43+
if(isComplexType || isEnumerableType)
44+
{
45+
output.SuppressOutput();
46+
return;
47+
}
48+
49+
// Attempt to fetch/cast the Model as IPublishedContent
50+
var contentNode = ViewContext.ViewData.Model as IPublishedContent;
51+
if(contentNode == null)
52+
{
53+
output.SuppressOutput();
54+
return;
55+
}
56+
57+
output.TagName = ""; // Remove the outer <our-fallback>
58+
59+
// Get the Model property with fallback mode
60+
var result = contentNode.Value(ModelProperty.Name, fallback: Mode, culture: CultureCode);
61+
62+
// If we have a value that can be
63+
if (string.IsNullOrWhiteSpace($"{result}") == false)
64+
{
65+
output.Content.SetHtmlContent($"{result}");
66+
return;
67+
}
68+
}
69+
}
70+
}

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,18 @@ It can be used in one of two ways, either by specifying the `src` attribute to a
187187
<our-svg media-item="@Model.Logo" />
188188
```
189189

190+
## `<our-fallback>`
191+
This tag helper element `<our-fallback>` uses the same fallback mode logic that is only available on the `Value()` method of the `IPublishedContent` interface that uses a string for the property name to lookup. In addition if the fallback value from a language or ancestors is not available we are still able to fallback to the content inside the tag.
192+
193+
```cshtml
194+
@* Current way *@
195+
@Model.Value("Header", fallback:Fallback.ToLanguage)
196+
197+
<h3><our-fallback property="Header" mode="Fallback.ToLanguage" culture="da-DK">I do NOT have a DK culture variant of this property</our-fallback></h3>
198+
<h3><our-fallback property="Header" mode="Fallback.ToAncestors">I do NOT have a Header property set on ANY parent and ancestors</our-fallback></h3>
199+
```
200+
201+
190202
## Video 📺
191203
[![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)
192204

0 commit comments

Comments
 (0)