2

I need separate image fields for mobile and desktop and trying to render it using Scriban in picture tag as below.

Current Scriban code:

<picture>
     <source srcset="{{ i_item.MyMobileImage.media_url }}" media="(max-width: 767px)">
     <source srcset="{{ i_item.MyDesktopImage.media_url }}" media="(min-width: 768px)">
</picture>

Is there a OOTB way to get image url with specific width and media protection hash using Scriban? or Writing Scriban extension is the solution?

1

I don't think there is an ootb way to do this (yet). A small extension could help you here. Something like this (not tested):

public class MediaUrl : IGenerateScribanContextProcessor
{
    private readonly IContext context;

    public MediaUrl(IContext context)
    {
        this.context = context;
    }

    public void Process(GenerateScribanContextPipelineArgs args)
    {
        var url = new GetMediaUrl(GetUrl);
        args.GlobalScriptObject.Import("sc_fullmediaurl", (Delegate)url);
    }

    public string GetUrl(Item item, string field, string width)
    {
        var options = new MediaUrlOptions { ... };
        var url = MediaManager.GetMediaUrl(mediaItem, options);
        return HashingUtils.ProtectAssetUrl(url);
    }

    private delegate string GetMediaUrl(Item item, string field, string width);
}

Another option could be to use sc_execute and a responsive image variant. This way you use the ootb functionality of SXA to get responsive images instead of having 2 image fields.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.