Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 54 additions & 11 deletions src/Umbraco.Web.Common/Extensions/BlockGridTemplateExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ 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)
{
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)
Expand All @@ -33,6 +34,54 @@ public static async Task<IHtmlContent> GetBlockGridHtmlAsync(this IHtmlHelper ht
=> 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);

Expand All @@ -43,18 +92,12 @@ public static async Task<IHtmlContent> 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<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);
}
51 changes: 42 additions & 9 deletions src/Umbraco.Web.Common/Extensions/BlockListTemplateExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,41 @@ 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)
{
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)
Expand All @@ -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))
{
Expand All @@ -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;
}
}