0

I have a CustomHeroViewModel class

public class CustomHeroViewModel : VariantsRenderingModel
{
    public string HeroTitle { get; set; }
    public string HeroDescription { get; set; }
    public List<string> ImageUrl { get; set; }
}

I am using the Model field in the Rendering Variant and I am able to get HeroTitle and HeroDescription Model fields in the Rendering Variant.

enter image description here

ImageUrl field is the List of string. How can I loop through ImageUrl and use it in the Rendering Variant?

Has anyone done something similar to that? Any help or recommendation would be appreciated

Thanks in advance

4
  • What version of SXA are you using? – Richard Seal Jul 21 '20 at 12:53
  • 11000 @RichardSeal – user2980 Jul 21 '20 at 13:00
  • have a look at this article - olmecdev.com/2019/The-SXA-Way-Advanced-Rendering-Variants – Gaurav Agarwal Jul 21 '20 at 13:39
  • @GauravAgarwal thanks that answer my question. I also tried to use Scriban as well. I wanted to print the HeroTitle property like {{o_model.HeroTitle}} but it shows no result. Any thoughts? – user2980 Jul 21 '20 at 13:52
0

I'm afraid this might not be possible at the moment. This feature initially was designed to work with strongly typed models so you can work around it by having:

public class CustomHeroViewModel : VariantsRenderingModel
{
    public string HeroTitle { get; set; }
    public string HeroDescription { get; set; }
    public List<MyImage> Images { get; set; }
}

public class MyImage
{
    public string SomeName { get; set; }
    public string ImageUrl { get; set; }
}

and use Model Iterator variant item to iterate over the Images property. Like this:

enter image description here

The other option is to override the Sitecore.XA.Foundation.RenderingVariants.Services.PropertyExtractor and handle List<string>.

4
  • thanks for answering my question. I also tried to use Scriban as well. I wanted to print the HeroTitle property like {{o_model.HeroTitle}} but it shows no result. Any thoughts? – – user2980 Jul 21 '20 at 13:56
  • Can we do the same thing using Scriban? – user2980 Jul 21 '20 at 13:57
  • @OwaisAhmed Yes, it is possible. I added an answer to your second question. – Dawid Rutkowski Jul 22 '20 at 11:00
  • How can i get the result count in the Model Iterator, depending on the count we want to show different look and feel – Karthik Raghavan Dec 21 '20 at 14:33

Your Answer

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