It's also not a bad idea to have a ViewComponent responsible for rendering all your img / picture tags from your razor, so 'one place' to update the logic around the displaying of images in a consistent way, eg if you want to provide responsive srcsets etc - then your DisplayLovelyImage method would be:
With ViewComponents, there are two parts, the 'code' and the 'view'
You create a class that inherits from ViewComponent, so for example, we tend to use Slimsy a lot on sites, and so this ViewComponent builds up different variations of the Url, low quality image etc for lazyish loading
public class ResponsiveImageViewComponent : ViewComponent
{
public async Task<IViewComponentResult> InvokeAsync(MediaWithCrops image, UrlHelper urlHelper, int width, int height, string cssClassName = "")
{
var imageModel = new ResponsiveImageViewModel();
if (image != null)
{
string defaultMimeType;
var umbracoExtension = image.Value<string>(Constants.Conventions.Media.Extension);
var defaultFormat = umbracoExtension;
switch (umbracoExtension)
{
case "jpg":
defaultMimeType = "image/jpeg";
break;
case "png":
defaultMimeType = "image/png";
break;
case "gif":
defaultMimeType = "image/gif";
break;
default:
defaultMimeType = "image/jpeg";
defaultFormat = "jpg";
break;
}
var lqipWidth = (int)Math.Round((decimal)width / 2);
var lqipHeight = (int)Math.Round((decimal)height / 2);
var imgSrcSet = urlHelper.GetSrcSetUrls(image, width, height, outputFormat: defaultFormat);
var imgSrc = SlimsyExtensions.GetCropUrl(image, width, height, furtherOptions: "&format=" + defaultFormat);
// ** Using half width/height for LQIP to reduce filesize to a minimum, CSS must oversize the images **
var imgLqip = SlimsyExtensions.GetCropUrl(image, lqipWidth, lqipHeight, quality: 20, furtherOptions: "&format=" + defaultFormat);
//Image Model Stuff
imageModel.DefaultSrcSetUrl = imgSrcSet;
imageModel.Id = image.Id;
imageModel.Url = imgSrc;
imageModel.CssClass = cssClassName;
imageModel.LqipUrl = imgLqip;
imageModel.AltText = image.Name;
}
return View("ResponsiveImage", imageModel);
}
}
Then you create a corresponding View in /Views/Components/ResponsiveImage/ResponsiveImage.cshtml
Best practice to render optional links
I'm wondering what is best practice, when you have to deal with optional links/containers in html.
For example: I have a list of elements. Each element has an image field, and a Url-picker field. The Url-picker value is optional.
So I loop through the elements, to render the html like this:
How can I make the anchor optional?
I can't do:
Because then I open an -tag without closing in the same block.
In Umbraco7 I did this:
But the UmbracoHelper doesn't seem to have an 'if' function anymore.
What's the best solution for this problem?
Hi Jeroen
Number of ways you could look at this...
You could create a razor helper to write out the image bit eg
and then in your foreach have:
It's also not a bad idea to have a ViewComponent responsible for rendering all your img / picture tags from your razor, so 'one place' to update the logic around the displaying of images in a consistent way, eg if you want to provide responsive srcsets etc - then your DisplayLovelyImage method would be:
regards
Marc
That's a nice way indeed. I'm still not too familiar with components, but I might give that a try too.
Thank you.
Hi Jeroen
This is a good intro: https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-6.0#add-a-viewcomponent-class
With ViewComponents, there are two parts, the 'code' and the 'view'
You create a class that inherits from ViewComponent, so for example, we tend to use Slimsy a lot on sites, and so this ViewComponent builds up different variations of the Url, low quality image etc for lazyish loading
Then you create a corresponding View in /Views/Components/ResponsiveImage/ResponsiveImage.cshtml
Which would then write out the img tag
if that gives you the idea!
regards
marc
is working on a reply...