Build a rendering that includes variants
To create a new rendering that includes variants, you create a custom rendering, but use different base classes for controllers and repositories.
In the Presentation folder of your site, you can set which renderings are visible in the Toolbox in the Experience Editor. To be able to use the new custom rendering with its rendering variants, you must create the item using the exact same name under both the Available Renderings and the RenderingVariants folder.
This topic describes the changes needed in the code for renderings that include variants for the controller, repository, model, and view classes.
Controller
Controller
After you create a new rendering using SXA's basic structure, you must create a controller. For a rendering that includes variants, instead of inheriting from the StandardController class, the controller must inherit from the VariantsController.
For example, for a custom rendering called PageTitle the controller could look like this:
Repository
Repository
The VariantsController provides the VariantsRepository:
Model
Model
The model class of the new rendering must inherit from the VariantsRenderingModel:
View
View
To generate the HTML markup in your view rendering cshtml file, iterate through the item properties of the model.
For example: