diff --git a/src/Umbraco.Web.Common/Extensions/BlockGridTemplateExtensions.cs b/src/Umbraco.Web.Common/Extensions/BlockGridTemplateExtensions.cs index 4cae63426bfe..c8afbf0e68ea 100644 --- a/src/Umbraco.Web.Common/Extensions/BlockGridTemplateExtensions.cs +++ b/src/Umbraco.Web.Common/Extensions/BlockGridTemplateExtensions.cs @@ -15,6 +15,8 @@ public static class BlockGridTemplateExtensions public const string DefaultItemsTemplate = "items"; public const string DefaultItemAreasTemplate = "areas"; + #region Async + public static async Task GetBlockGridHtmlAsync(this IHtmlHelper html, BlockGridModel? model, string template = DefaultTemplate) { if (model?.Count == 0) @@ -22,8 +24,7 @@ public static async Task GetBlockGridHtmlAsync(this IHtmlHelper ht return new HtmlString(string.Empty); } - var view = $"{DefaultFolder}{template}"; - return await html.PartialAsync(view, model); + return await html.PartialAsync(DefaultFolderTemplate(template), model); } public static async Task GetBlockGridHtmlAsync(this IHtmlHelper html, IPublishedProperty property, string template = DefaultTemplate) @@ -33,6 +34,54 @@ public static async Task GetBlockGridHtmlAsync(this IHtmlHelper ht => await GetBlockGridHtmlAsync(html, contentItem, propertyAlias, DefaultTemplate); public static async Task GetBlockGridHtmlAsync(this IHtmlHelper html, IPublishedContent contentItem, string propertyAlias, string template) + { + IPublishedProperty prop = GetRequiredProperty(contentItem, propertyAlias); + return await GetBlockGridHtmlAsync(html, prop.GetValue() as BlockGridModel, template); + } + + public static async Task GetBlockGridItemsHtmlAsync(this IHtmlHelper html, IEnumerable items, string template = DefaultItemsTemplate) + => await html.PartialAsync(DefaultFolderTemplate(template), items); + + public static async Task GetBlockGridItemAreasHtmlAsync(this IHtmlHelper html, BlockGridItem item, string template = DefaultItemAreasTemplate) + => await html.PartialAsync(DefaultFolderTemplate(template), item); + + #endregion + + #region Sync + + public static IHtmlContent GetBlockGridHtml(this IHtmlHelper html, BlockGridModel? model, string template = DefaultTemplate) + { + if (model?.Count == 0) + { + return new HtmlString(string.Empty); + } + + return html.Partial(DefaultFolderTemplate(template), model); + } + + public static IHtmlContent GetBlockGridHtml(this IHtmlHelper html, IPublishedProperty property, string template = DefaultTemplate) + => GetBlockGridHtml(html, property.GetValue() as BlockGridModel, template); + + public static IHtmlContent GetBlockGridHtml(this IHtmlHelper html, IPublishedContent contentItem, string propertyAlias) + => GetBlockGridHtml(html, contentItem, propertyAlias, DefaultTemplate); + + public static IHtmlContent GetBlockGridHtml(this IHtmlHelper html, IPublishedContent contentItem, string propertyAlias, string template) + { + IPublishedProperty prop = GetRequiredProperty(contentItem, propertyAlias); + return GetBlockGridHtml(html, prop.GetValue() as BlockGridModel, template); + } + + public static IHtmlContent GetBlockGridItemsHtml(this IHtmlHelper html, IEnumerable items, string template = DefaultItemsTemplate) + => html.Partial(DefaultFolderTemplate(template), items); + + public static IHtmlContent GetBlockGridItemAreasHtml(this IHtmlHelper html, BlockGridItem item, string template = DefaultItemAreasTemplate) + => html.Partial(DefaultFolderTemplate(template), item); + + #endregion + + private static string DefaultFolderTemplate(string template) => $"{DefaultFolder}{template}"; + + private static IPublishedProperty GetRequiredProperty(IPublishedContent contentItem, string propertyAlias) { ArgumentNullException.ThrowIfNull(propertyAlias); @@ -43,18 +92,12 @@ public static async Task GetBlockGridHtmlAsync(this IHtmlHelper ht nameof(propertyAlias)); } - IPublishedProperty? prop = contentItem.GetProperty(propertyAlias); - if (prop == null) + IPublishedProperty? property = contentItem.GetProperty(propertyAlias); + if (property == null) { throw new InvalidOperationException("No property type found with alias " + propertyAlias); } - return await GetBlockGridHtmlAsync(html, prop.GetValue() as BlockGridModel, template); + return property; } - - public static async Task GetBlockGridItemsHtmlAsync(this IHtmlHelper html, IEnumerable items, string template = DefaultItemsTemplate) - => await html.PartialAsync($"{DefaultFolder}{template}", items); - - public static async Task GetBlockGridItemAreasHtmlAsync(this IHtmlHelper html, BlockGridItem item, string template = DefaultItemAreasTemplate) - => await html.PartialAsync($"{DefaultFolder}{template}", item); } diff --git a/src/Umbraco.Web.Common/Extensions/BlockListTemplateExtensions.cs b/src/Umbraco.Web.Common/Extensions/BlockListTemplateExtensions.cs index 17b620ab51ed..edf3055159fd 100644 --- a/src/Umbraco.Web.Common/Extensions/BlockListTemplateExtensions.cs +++ b/src/Umbraco.Web.Common/Extensions/BlockListTemplateExtensions.cs @@ -10,6 +10,33 @@ public static class BlockListTemplateExtensions public const string DefaultFolder = "blocklist/"; public const string DefaultTemplate = "default"; + #region Async + + public static async Task GetBlockListHtmlAsync(this IHtmlHelper html, BlockListModel? model, string template = DefaultTemplate) + { + if (model?.Count == 0) + { + return new HtmlString(string.Empty); + } + + return await html.PartialAsync(DefaultFolderTemplate(template), model); + } + + public static async Task GetBlockListHtmlAsync(this IHtmlHelper html, IPublishedProperty property, string template = DefaultTemplate) + => await GetBlockListHtmlAsync(html, property.GetValue() as BlockListModel, template); + + public static async Task GetBlockListHtmlAsync(this IHtmlHelper html, IPublishedContent contentItem, string propertyAlias) + => await GetBlockListHtmlAsync(html, contentItem, propertyAlias, DefaultTemplate); + + public static async Task GetBlockListHtmlAsync(this IHtmlHelper html, IPublishedContent contentItem, string propertyAlias, string template) + { + IPublishedProperty property = GetRequiredProperty(contentItem, propertyAlias); + return await GetBlockListHtmlAsync(html, property.GetValue() as BlockListModel, template); + } + #endregion + + #region Sync + public static IHtmlContent GetBlockListHtml(this IHtmlHelper html, BlockListModel? model, string template = DefaultTemplate) { if (model?.Count == 0) @@ -17,8 +44,7 @@ public static IHtmlContent GetBlockListHtml(this IHtmlHelper html, BlockListMode return new HtmlString(string.Empty); } - var view = DefaultFolder + template; - return html.Partial(view, model); + return html.Partial(DefaultFolderTemplate(template), model); } public static IHtmlContent GetBlockListHtml(this IHtmlHelper html, IPublishedProperty property, string template = DefaultTemplate) @@ -29,10 +55,17 @@ public static IHtmlContent GetBlockListHtml(this IHtmlHelper html, IPublishedCon public static IHtmlContent GetBlockListHtml(this IHtmlHelper html, IPublishedContent contentItem, string propertyAlias, string template) { - if (propertyAlias == null) - { - throw new ArgumentNullException(nameof(propertyAlias)); - } + IPublishedProperty property = GetRequiredProperty(contentItem, propertyAlias); + return GetBlockListHtml(html, property.GetValue() as BlockListModel, template); + } + + #endregion + + private static string DefaultFolderTemplate(string template) => $"{DefaultFolder}{template}"; + + private static IPublishedProperty GetRequiredProperty(IPublishedContent contentItem, string propertyAlias) + { + ArgumentNullException.ThrowIfNull(propertyAlias); if (string.IsNullOrWhiteSpace(propertyAlias)) { @@ -41,12 +74,12 @@ public static IHtmlContent GetBlockListHtml(this IHtmlHelper html, IPublishedCon nameof(propertyAlias)); } - IPublishedProperty? prop = contentItem.GetProperty(propertyAlias); - if (prop == null) + IPublishedProperty? property = contentItem.GetProperty(propertyAlias); + if (property == null) { throw new InvalidOperationException("No property type found with alias " + propertyAlias); } - return GetBlockListHtml(html, prop.GetValue() as BlockListModel, template); + return property; } }