-
-
Save bewards/66159aad4ce0244719afbc474c153653 to your computer and use it in GitHub Desktop.
Sitecore RenderSVG HtmlHelper for svg-inject
This file contains 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 Sitecore; | |
using Sitecore.Data.Fields; | |
using Sitecore.Data.Items; | |
using Sitecore.Resources.Media; | |
using System; | |
using System.IO; | |
using System.Web.Mvc; | |
namespace Foundation.SitecoreHelpers.Extensions | |
{ | |
public static class HtmlHelpers | |
{ | |
public static MvcHtmlString RenderSvg2(this HtmlHelper htmlHelper, ImageField image) | |
{ | |
if (image == null || image.MediaItem == null) { return new MvcHtmlString($"<!-- Empty {(image == null ? "Image Field" : "Media Item")} -->"); } | |
var imageMediaItem = new MediaItem(image.MediaItem); | |
if (Sitecore.Context.Item.Editing.IsEditing || imageMediaItem.MimeType != "image/svg+xml") { | |
return new MvcHtmlString(String.Format("<img src=\"{0}\" alt=\"{1}\" />", StringUtil.EnsurePrefix('/', MediaManager.GetMediaUrl(imageMediaItem)), image.Alt)); | |
} | |
return new MvcHtmlString(string.Format("<img src=\"{0}\" alt=\"{1}\" onload=\"SVGInject(this)\" />", StringUtil.EnsurePrefix('/', MediaManager.GetMediaUrl(imageMediaItem)), image.Alt)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment