DFO 4.0.0 - 4 Referencing and Rendering Assets
Assets from Digizuite can be rendered like any other assets in Optimizely either in Content Areas (using the included default controllers) or in XHTML properties or properties are either of the type ContentReference or Url.
When rendered in a Razor view, you can either use the "PropertyFor" method - but you can also use one of several extension methods to retrieve the URL for the asset and use that directly in your rendering.
Referencing Assets in Content Models
When you create the properties in your content models, you can either make a ContentReference to hold the reference to them, or a Url. If you assign the proper UIHint, the same editorial experience will be shown.
//Image Selector
[UIHint(UIHint.Image)]
public virtual ContentReference TestImage { get; set; }
//Same image selector as above
[UIHint(UIHint.Image)]
public virtual Url AnotherTestImageWithUrl { get; set; }
//This will allow the editor to pick any URL or asset
public virtual Url UrlThatCanBeAnImage { get; set; }
//Video selector
[UIHint(UIHint.Video)]
public virtual ContentReference TestVideo { get; set; }
The code above will be shown to the editor as seen below.
Notice how Digizuite has it's own selector in the Add Link dropdown.
Default Controllers
As mentioned earlier, just as the integration comes with default models, it also comes with default controllers so that it will work out-of-the-box when you drag in Digizuite assets into for example a content area. They exist for both images and videos.
They are an absolute minimum and can be overwritten if/when needed. One thing they do feature is that they will check if they are currently rendered with a rendering tag - and if so, trying to resolve the proper Digizuite media format for that rendering tag.
Here is the main method from the default Video File Controller:
[TemplateDescriptor(AvailableWithoutTag = true, Inherited = true)]
public class DefaultVideoAssetController : PartialContentController<IDigizuiteVideo>
{
public DefaultVideoAssetController()
{
}
public override ActionResult Index(IDigizuiteVideo currentContent)
{
var tag = ControllerContext.ParentActionViewContext.ViewData["tag"] as string;
string ub = currentContent.ContentLink.DigizuiteMediaUrl(tag);
return Content($"<video controls autoplay name=\"media\" style=\"width: 100%;\"><source src=\"{ub}\" type=\"video/mp4\"></video>");
}
}
Using Rendering Tags
A common task is to ensure that the proper media format for any given media asset is delivered. The media format can either be directly controlled in code, as we will see further down, it can be managed by editors in the XHTML fields, but you can also set up rules as a developer to connect it with Optimizely's rendering tags - just like the default controllers above takes into consideration. Read more about the rendering tags here: DFO 4.0.0 - 7 Dealing with Media Formats and Rendering Tags
Understanding URLs
As mentioned in the chapter DFO 4.0.0 - 2 Modelling Asset Content Types, you can assign Blob properties on your custom models to different media formats, so that when you render them you can simply call /[path to asset]/[Blob property name]. So, if you had a small media format, mapped to a "Small" property blob on the content model, you could simply call /[path to asset]/Small to get the small media format. In fact, the default Image class contains both Thumbnail and Preview.
There are also some hardcoded options you can add to the URL, namely:
"/Download" to download the asset.
"/Source" to fetch the original source of the asset. This comes in handy when using SVG logos, where you typically want the source, rather than an image format.
It's also possible to specify the media format id directly by adding a url parameter called "mediaformatid", E.g. /globalassets/en/digizuite/5272-blueberry.jpg?mediaformatid=50034.
Extension Methods
To make it both faster and easier to generate the urls needed, we have added a few useful helper methods.
Getting the right media format
To fetch the right media formats we have created some extension methods that come in handy.
UrlHelper extension
We have extended the UrlHelper in MVC, so you can easily in your razor templates use it, not only to get the ContentUrl, but also to get the url to an asset with the proper media format.
public static string DigizuiteMediaUrl(this UrlHelper urlHelper, IContentData model, Expression<Func<IContentData, ContentReference>> propertyExpression, object additionalParam = null)
The additionalParam can be a
formatid: 50034 | 50035 | 50036 | 50037
e.g. <img src="@Url.DigizuiteMediaUrl(Model.CurrentPage, x => Model.CurrentPage.BannerImage, 50034)" />
formatname: Thumbnail | Preview
e.g. <img src="@Url.DigizuiteMediaUrl(Model.CurrentPage, x => Model.CurrentPage.ImageFormatMedium, "Preview")" />
tagging: span12 | span8 | span6 | span4
e.g. <img src="@Url.DigizuiteMediaUrl(Model.CurrentPage, x => Model.CurrentPage.MainImage, Global.ContentAreaTags.OneThirdWidth)" />
Default media format: image = 3, video = 10079
e.g. <img src="@Url.GetMediaUrl(Model.CurrentPage, x => Model.CurrentPage.ImageFormatBig)" />
Priority to get specific media format
Through the MediaFormat pass as parameter to extension
Through the MediaFormat attribute on the content model
Through tagmapping pass as parameter to extension (in our example case, there is a rule set up in initialization that if the rendering tag is "span4" then it should use mediaformat small for images)
If nothing is defined we fall back to the default mediaformat (which should be configurable - and can vary based on file type)
As site developer we can get media url via content reference extension where the format return base on name, id or tagging
public static string DigizuiteMediaUrl(this ContentReference contentReference, object additionalParam = null)
e.g.
var tag = ControllerContext.ParentActionViewContext.ViewData["tag"] as string;
string ub = currentContent.ContentLink.DigizuiteMediaUrl(tag);
Optimizely UrlExtensions
If you use the default Optimizely url extension like this @Url.ContentUrl(@Model.DemoImage)
. It will render a url like this /globalassets/en/digizuite/5272-blueberry.jpg. This will return the default format.
ContentReference extension
The extension above also comes in a similar version for ContentReference - so if you have a contentreference to an asset, you can use it to get the url - either with a format ID, a format Name or a tag:
public static string DigizuiteMediaUrl(this ContentReference contentReference, object additionalParam = null)
Retrieving a Crop
Likewise, you might need to find a specific cropped version on an asset in code, and using this code you can do just that:
public static ContentReference GetCrop(this ContentReference contentReference, string cropName)
e.g. <img src="@Url.ContentUrl(Model.CurrentPage.ImageFormatSmall.GetCrop("banner"))" />
Combine get crop and media format by Name, Id or a tagging
e.g. <img src="@Model.CurrentPage.ImageFormatSmall.GetCrop("square").DigizuiteMediaUrl("Small")" />
e.g. <img src="@Model.CurrentPage.ImageFormatSmall.GetCrop("square").DigizuiteMediaUrl(50036)" />