Add sync rendering extensions for block grid and async ones for block list (#13168)

This commit is contained in:
Kenn Jacobsen
2022-10-12 10:06:43 +02:00
committed by GitHub
parent 7777ba40fd
commit b8c73e7550
2 changed files with 96 additions and 20 deletions

View File

@@ -15,6 +15,8 @@ public static class BlockGridTemplateExtensions
public const string DefaultItemsTemplate = "items";
public const string DefaultItemAreasTemplate = "areas";
#region Async
public static async Task<IHtmlContent> GetBlockGridHtmlAsync(this IHtmlHelper html, BlockGridModel? model, string template = DefaultTemplate)
{
if (model?.Count == 0)
@@ -22,8 +24,7 @@ public static class BlockGridTemplateExtensions
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<IHtmlContent> GetBlockGridHtmlAsync(this IHtmlHelper html, IPublishedProperty property, string template = DefaultTemplate)
@@ -33,6 +34,54 @@ public static class BlockGridTemplateExtensions
=> await GetBlockGridHtmlAsync(html, contentItem, propertyAlias, DefaultTemplate);
public static async Task<IHtmlContent> 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<IHtmlContent> GetBlockGridItemsHtmlAsync(this IHtmlHelper html, IEnumerable<BlockGridItem> items, string template = DefaultItemsTemplate)
=> await html.PartialAsync(DefaultFolderTemplate(template), items);
public static async Task<IHtmlContent> 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<BlockGridItem> 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 class BlockGridTemplateExtensions
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<IHtmlContent> GetBlockGridItemsHtmlAsync(this IHtmlHelper html, IEnumerable<BlockGridItem> items, string template = DefaultItemsTemplate)
=> await html.PartialAsync($"{DefaultFolder}{template}", items);
public static async Task<IHtmlContent> GetBlockGridItemAreasHtmlAsync(this IHtmlHelper html, BlockGridItem item, string template = DefaultItemAreasTemplate)
=> await html.PartialAsync($"{DefaultFolder}{template}", item);
}

View File

@@ -10,6 +10,33 @@ public static class BlockListTemplateExtensions
public const string DefaultFolder = "blocklist/";
public const string DefaultTemplate = "default";
#region Async
public static async Task<IHtmlContent> 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<IHtmlContent> GetBlockListHtmlAsync(this IHtmlHelper html, IPublishedProperty property, string template = DefaultTemplate)
=> await GetBlockListHtmlAsync(html, property.GetValue() as BlockListModel, template);
public static async Task<IHtmlContent> GetBlockListHtmlAsync(this IHtmlHelper html, IPublishedContent contentItem, string propertyAlias)
=> await GetBlockListHtmlAsync(html, contentItem, propertyAlias, DefaultTemplate);
public static async Task<IHtmlContent> 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 class BlockListTemplateExtensions
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 class BlockListTemplateExtensions
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 class BlockListTemplateExtensions
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;
}
}