Created
January 11, 2023 10:52
-
-
Save smdooley/217ba3fc060d6eac7ecb20836525fdeb to your computer and use it in GitHub Desktop.
Umbraco related HTML helper extension methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Text; | |
using Microsoft.AspNetCore.Html; | |
using Microsoft.AspNetCore.Mvc.Rendering; | |
using Microsoft.AspNetCore.Mvc.ViewFeatures; | |
using Umbraco.Cms.Core.Models; | |
using Umbraco.Extensions; | |
namespace Concept.Core.Extensions | |
{ | |
public static class HtmlHelperExtensions | |
{ | |
public static HtmlString ResponsiveImage(this IHtmlHelper helper, MediaWithCrops media, string defaultCrop = "xs", object? htmlAttributes = null) | |
{ | |
TagBuilder tagBuilder = new TagBuilder("img") | |
{ | |
TagRenderMode = TagRenderMode.SelfClosing | |
} | |
var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes); | |
tagBuilder.MergeAttributes(attributes); | |
tagBuilder.MergeAttribute("src", string.IsNullOrWhiteSpace(defaultCrop) | |
? media.GetCropUrl(furtherOptions: "&format=webp") | |
: media.GetCropUrl(cropAlias: defaultCrop, useCropDimensions: true, furtherOptions: "&format=webp")); | |
if(media.LocalCrops == null) return new HtmlString(tagBuilder.ToHtmlString()); | |
tagBuilder.MergeAttribute("srcset", media.GetSrcSet().ToString()); | |
return new HtmlString(tagBuilder.ToHtmlString()); | |
} | |
public static HtmlString ResponsiveImage(this IHtmlHelper helper, MediaWithCrops media, string defaultCrop = "xs", string sizes = "100vw", object? htmlAttributes = null) | |
{ | |
TagBuilder tagBuilder = new TagBuilder("img") | |
{ | |
TagRenderMode = TagRenderMode.SelfClosing | |
} | |
var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes); | |
tagBuilder.MergeAttributes(attributes); | |
tagBuilder.MergeAttribute("src", string.IsNullOrWhiteSpace(defaultCrop) | |
? media.GetCropUrl(furtherOptions: "&format=webp") | |
: media.GetCropUrl(cropAlias: defaultCrop, useCropDimensions: true, furtherOptions: "&format=webp")); | |
if(media.LocalCrops == null) return new HtmlString(tagBuilder.ToHtmlString()); | |
tagBuilder.MergeAttribute("srcset", media.GetSrcSet().ToString()); | |
if(sizes.IsNullOrWhiteSpace()) return new HtmlString(tagBuilder.ToHtmlString()); | |
tagBuilder.MergeAttributes("sizes", sizes); | |
return new HtmlString(tagBuilder.ToHtmlString()); | |
} | |
public static HtmlString GetSrcSet(this MediaWithCrops media) | |
{ | |
StringBuilder sb = new StringBuilder(string.Empty); | |
foreach(var crop in media.LocalCrops.Crops) | |
{ | |
sb.Append(media.GetCropUrl(cropAlias: crop.Alias, useCropDimensions: true, furtherOptions: "&format=webp")); | |
sb.Append(" "); | |
sp.Append(crop.Width); | |
sp.Append("w"); | |
if(crop != media.LocalCrops.Crops.Last()) | |
{ | |
sb.Append(","); | |
} | |
} | |
return (new HtmlString(sb.ToString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment