I managed to create a custom rendering in SXA by inheriting my controller from the StandardController
(and some other things). But now I would like to create a component that has the ability to work with variants. I saw that there is a VariantsController
, so I assume that my first step is to use that as base for my custom controller, but what are the other steps I need to take to have 2 variants of my rendering? And what are the options in my view with these variants - what can be altered?
1 Answer
Yes, you should inherit from VariantsController
. It's very small and all what it's doing is providing VariantsRepository
(in SXA almost all of logic is placed inside of repositories). It got all what you need to enable your rendering using Rendering Variants.
So the steps you be as follow:
- Your rendering parameters template should inherit from IComponentVariant
- Your repository should inherit from VariantsRepository
- Your model which you push to your view should inherit from VariantsRenderingModel
- Render content using SXA helpers
@Html.RenderingVariants().RenderVariant()
. You can take a look at views of Page Content or Title renderings which are quite simple.