Responisve Image in SXA
Using Rendering Variants
Having different images to optimize the experience on different devices (or screen resolutions) is essential. Here's a quick implementation example on how to achieve that using Rendering Variants (Scriban or Items).
If you check an example on w3schools the desired markup to handle different images per screen resolutions has to look like this:
<picture>
<source srcset="img_smallflower.jpg" media="(max-width: 600px)">
<source srcset="img_flowers.jpg" media="(max-width: 1500px)">
<source srcset="flowers.jpg">
<img src="img_smallflower.jpg" alt="Flowers">
</picture>
1. General Setup
1.1 Create Module
- Navigate to sitecore/System/Settings/Feature
- Create a Folder using Insert Options for your Solution
- On "Feature" node use insert Option to create a Module
- Fill in the Module Name
- Choose the Module Group (the folder you just created)
- Select the areas you want to create items in for this module
- Select the Module Scaffolding actions to make the module available on tenant and/or site creation
1.2 Clone Rendering
My Rendering should use a data source to be created or reused from my site/data Folder or sharedsite/data Folder. As the promo component has the same behaviour I'm using this component to clone from
- Navigate to /sitecore/layout/Renderings/Feature/Experience Accelerator/Page Content/Promo
- Perfrom a right click on the promo rendering item to enter the context menu and choose Scripts --> Clone Rendering
- Provide a Rendering Name
- Choose the the Module you just created
- Provide a css class that is used on the <div> that wrapps the whole component.
- On the second tab "Parameters" choose "Make a copy of original rendering parameters" to not inherit from promo rendering parameters
- On third tab "Datasource" choose ""Make a copy of original datasource" to not inherit from promo datasource template
- On foruth tab you can create a new view file.
1.3 Adjust Template
The script that ran to clone your rendering has created a set of templates in the folder created by your module.
- Navigate to /sitecore/templates/Feature/SXA Tutorial/Responisve Image to find the created templates
- Navigate to "Responsive Image" Template to adjust the fields
- Create the image fields you want to use in the responsive image (DesktopImage; TabletImage;MobileImage)
- Don't forget to set the source field to at least query:$siteMedia
1.4 Create Experience Editor Button
To improve the Author Experience to maintain all images easily from Experience Editor add an Experience Button.
- Switch to "Core" database (Launchpad -->Desktop --> select "Core" in bottom right corner)
- Enter the Content Editor
- Navigate to /sitecore/content/Applications/WebEdit/Custom Experience Buttons
- Add a new Item of template /sitecore/templates/System/WebEdit/Field Editor Button
- Provide Header Tooltip and Icon
- In the "Fields" field add the fields to be shown in the dialog pipe-separated
- Go back to Master database (Content Editor)
- Navigate to /sitecore/layout/Renderings/Feature/SXA Tutorial/Responisve Image/Responsive Image
- In the Field "Experience Editor Buttons" choose the created button
1.5 Add Rendering to Toolbox
- Navigate to <your site>/Presentation/Available Renderings
- Make sure on Available Renderings Item that checkbox is checked
- Choose an existing Available Renderings Item or create a custom one
- Add your Rendering
2. Scriban Variant Example
Note: Scriban requires SXA version 9.3 or later
Your Rendering is still missing a proper markup. You can directly create the Rendering Variants in your site or add it to the module. I will add it to the module to install all items through my module later.
As the module installs certain templates to a certain locations and rendering variants consist of multiple renderings new need to create your rendering variants in the branch templates.
- Navigate to /sitecore/templates/Branches/Feature/SXA Tutorial/Responisve Image/Default Responsive Image Variant
- Create a new Variant Definition item under the Variant item called "$name" with name e.g.: Responsive Image Scriban
- Add Scriban Renderer using insert options
- Add Code to template field:
<picture>
<source srcset="{{i_item.MobileImage.target.media_url}}" media="(max-width: 767px)">
<source srcset="{{i_item.TabletImage.target.media_url}}" media="(max-width: 991px)">
<source srcset="{{i_item.DesktopImage.target.media_url}}">
<img src="{{i_item.DesktopImage.target.media_url}}" alt="{{i_item.DesktopImage.target.alt}}">
</picture>
3. Item Variant Example
If you have not done Chapter 2 your Rendering is still missing a proper markup. You can directly create the Rendering Variants in your site or add it to the module. I will add it to the module to install all items through my module later.
3.1 Create custom tags
As the out of the box installation does not provide the tags <picture> and <soure> we need to create them.
3.1.1 <picture> tag
- Navigate to /sitecore/system/Settings/Foundation/Experience Accelerator/Rendering Variants/Enums/Tags
- Add Enum Item "picture"
3.1.2 <source> tag
- Navigate to /sitecore/system/Settings/Foundation/Experience Accelerator/Rendering Variants/Enums/Self Closing Tags
- Add Enum Item "source"
3.2 Create Variant Definition
As the module installs certain templates to a certain locations and rendering variants consist of multiple renderings new need to create your rendering variants in the branch templates.
- Navigate to /sitecore/templates/Branches/Feature/SXA Tutorial/Responisve Image/Default Responsive Image Variant
- Create a new Variant Definition item under the Variant item called "$name" with name e.g.: "Responsive Image"
- Add Section Renderer using insert options called "picture"
- Select "picture" tag on section rederer
- Underneath picture create HTML Tag Renderer called "source Mobile"
- Select "source" tag on HTML Tag Renderer
- Set Data attributes srcset using Field Token $(MobileImage) to reference the field of your datasource
- Set Data attributes media using "(max-width: 767px)"
- Duplicate the HTML Tag Renderer called "source Mobile" and name it "source Tablet"
- Duplicate the HTML Tag Renderer called "source Mobile" and name it "source Desktop"
- Add field renderer and name it "image"
- Set field "Field name to "DesktopImage" to reference the field of your datasource.
4. Install Module to site
Before you install the Module to your site make sure that the actions are properly defined. If one action is not properly defined e.g. missing item name, the module will abort at the broken action.
- Navigate to /sitecore/system/Settings/Feature/SXA Tutorial/Responisve Image/Responisve Image Site Setup/
- Check Add Available Renderings action item if Location, Teplate and Name field are set
- Check also Add Responsive Images Data Item
- Navigate to "Rendering Variants" Folder
- Check Responsive Image action
If everything is correctly set you can install the module to your site
- Navigate to your site in my case: /sitecore/content/SXA Material Creation/SXA Materials/SXA Materials Shared
- Right-click the site item and choose Scripts --> Add Site Module
- Select your module from the list (unselect the others if not needed)
- Hit "OK" button
- Check if following items have been created:
- "<your site>/Data/Responsive Image" Folder
- "<your site>/Presentation/Rendering Variants/Responsive Image" containing the variant definitions
- "<your site>/Presentation/Available Renderings/Custom Media" containing your rendering
5. Test your new rendering
- Create a page
- Open the page in Experience Editor
- Add Rendering to Page
- Use Experience Editor Button to edit your images
- Save changes
- Check your page with different screen resolutions
- Check HTML Markup
Created: 19.6.2020
SXA Scriban