Rendering Media
Info on rendering media items and imaging cropping
Templates (Views) can access items in the Media library to assist in displaying rich content like galleries.
In the following examples, we will be looking at rendering an Image
.
Image is only one of the 'types' of Media in Umbraco. The same principles apply to all Media Types. The properties available to render will be different from Media Type to Media Type. For example, a File
will not have a Width property.
Rendering a media item
A media item is not only a reference to a static file. Like content, it is a collection of fields, such as width, height, and file path. This means that accessing and rendering media in a Template is similar to rendering content.
Example 1: Accessing a Media Image IPublishedContent item based on the ID
An uploaded image in the Media library is based on the Media Type Image
which has a number of standard properties:
Name
Width & Height
Size
Type (based on file extension)
UmbracoFile (the path to the file or JSON data containing crop information)
These standard properties are pre-populated and set during the upload process. For example, this means that the width and height are calculated for you.
If you want to add further properties to use with your Media Item, edit the Image Media Type under Settings. In this example, we are going to retrieve an image from the Media section. Then we will render out an img
tag using the URL of the media item and use the Name as the value for the alt
attribute.
The Media item in the following sample will use a sample Guid (55240594-b265-4fc2-b1c1-feffc5cf9571
). This example is not using Models Builder.
But wait a second, Umbraco comes with Models Builder. This means that you can use strongly typed models for your media items if Models Builder is enabled (which it is by default).
Example 2: Accessing a Media Image ModelsBuilder item based on the ID
As with example one, we are accessing a MediaType image
using the same Guid assumption.
It is always worth having null-checks around your code when retrieving media in case the conversion fails or Media() returns null. This makes your code more robust.
Other Media Items such as File
File
Accessing other media items can be performed in the same way. The techniques are not limited to the Image
type, but it is one of the most common use cases.
Image Cropper
The Image Cropper can be used with Image
Media Types and is the default option for the umbracoFile
property on an Image
Media Type.
When working with the Image Cropper for an image the GetCropUrl
extension method is used to retrieve cropped versions of the image. Details of the Image Cropper property editor and other examples of using it can be found in the Image Cropper article. The following is an example to help you get started with the Image Cropper.
Example of using Image Cropper
This example assumes that you have set up a crop called square on your Image Cropper Data Type.
If you want the original, uncropped image, you can ignore the GetCropUrl extension method and use one of the previously discussed approaches as shown below.
More information
Last updated