1

I'm creating a custom component in sxa with rendering variants. I had a look at many examples where most of them are using the field name that's available in the page item.

<div @Html.Sxa().Component("page-title", Model.Attributes)>
    <div class="component-content">

        <h1>@Model.CustomProperty</h1>
        @foreach (BaseVariantField variantField in Model.VariantFields)
        {
            @Html.RenderingVariants().RenderVariant(variantField, Model.Item, Model.RenderingWebEditingParams)
        }
    </div>
</div>

In the above example i could see Model.Item being passed into the RenderVariant method but instead i would like to pass values directly from the rendering parameters.

Is it possible to make use of rendering parameters with sxa custom rendering variants component. I would like to have fields in the rendering parameters where i will give the data. I would like to make use of different variants on the data given on rendering parameters.

Kindly suggest best approach.

2

Yes, you can do it. You need to create a new variant item and write new variant renderer (or change the implementation of one existing variant renderer) which will use rendering parameters.

We have added two custom pipelines in SXA:

  • parseVariantFields - processors in this pipeline convert items into models which later on are used by renderers
  • renderVariantField - this operates on models created in the previous pipeline and renders output

Based on the above you can see that we are operating on data which is in the variant items, not in the rendering parameters. That's you need a custom renderer.

I would suggest you take a look at one of those to get more details about how to write those processors:

<parseVariantFields>
    <processor type="Sitecore.XA.Foundation.RenderingVariants.Pipelines.ParseVariantFields.ParseField, Sitecore.XA.Foundation.RenderingVariants" resolve="true" />
    <processor type="Sitecore.XA.Foundation.RenderingVariants.Pipelines.ParseVariantFields.ParseDate, Sitecore.XA.Foundation.RenderingVariants" resolve="true" />
    <processor type="Sitecore.XA.Foundation.RenderingVariants.Pipelines.ParseVariantFields.ParseSection, Sitecore.XA.Foundation.RenderingVariants" resolve="true" />
    <processor type="Sitecore.XA.Foundation.RenderingVariants.Pipelines.ParseVariantFields.ParseEditFrame, Sitecore.XA.Foundation.RenderingVariants" resolve="true" />
    <processor type="Sitecore.XA.Foundation.RenderingVariants.Pipelines.ParseVariantFields.ParseHtmlTag, Sitecore.XA.Foundation.RenderingVariants" resolve="true" />
    <processor type="Sitecore.XA.Foundation.RenderingVariants.Pipelines.ParseVariantFields.ParseTemplate, Sitecore.XA.Foundation.RenderingVariants" resolve="true" />
    <processor type="Sitecore.XA.Foundation.RenderingVariants.Pipelines.ParseVariantFields.ParseText, Sitecore.XA.Foundation.RenderingVariants" resolve="true" />
    <processor type="Sitecore.XA.Foundation.RenderingVariants.Pipelines.ParseVariantFields.ParseModel, Sitecore.XA.Foundation.RenderingVariants" resolve="true" />
    <processor type="Sitecore.XA.Foundation.RenderingVariants.Pipelines.ParseVariantFields.ParseToken, Sitecore.XA.Foundation.RenderingVariants" resolve="true" />
    <processor type="Sitecore.XA.Foundation.RenderingVariants.Pipelines.ParseVariantFields.ParseReference, Sitecore.XA.Foundation.RenderingVariants" resolve="true" />
    <processor type="Sitecore.XA.Foundation.RenderingVariants.Pipelines.ParseVariantFields.ParsePlaceholder, Sitecore.XA.Foundation.RenderingVariants" resolve="true" />
    <processor type="Sitecore.XA.Foundation.RenderingVariants.Pipelines.ParseVariantFields.ParseQuery, Sitecore.XA.Foundation.RenderingVariants" resolve="true" />
    <processor type="Sitecore.XA.Foundation.RenderingVariants.Pipelines.ParseVariantFields.ParseResponsiveImage, Sitecore.XA.Foundation.RenderingVariants" resolve="true" />
</parseVariantFields>
<renderVariantField>
    <processor type="Sitecore.XA.Foundation.RenderingVariants.Pipelines.RenderVariantField.RenderVariantField, Sitecore.XA.Foundation.RenderingVariants" resolve="true" />
    <processor type="Sitecore.XA.Foundation.RenderingVariants.Pipelines.RenderVariantField.RenderDate, Sitecore.XA.Foundation.RenderingVariants" resolve="true" />
    <processor type="Sitecore.XA.Foundation.RenderingVariants.Pipelines.RenderVariantField.RenderSection, Sitecore.XA.Foundation.RenderingVariants" resolve="true" />
    <processor type="Sitecore.XA.Foundation.RenderingVariants.Pipelines.RenderVariantField.RenderEditFrame, Sitecore.XA.Foundation.RenderingVariants" resolve="true" />
    <processor type="Sitecore.XA.Foundation.RenderingVariants.Pipelines.RenderVariantField.RenderHtmlTag, Sitecore.XA.Foundation.RenderingVariants" resolve="true" />
    <processor type="Sitecore.XA.Foundation.RenderingVariants.Pipelines.RenderVariantField.RenderText, Sitecore.XA.Foundation.RenderingVariants" resolve="true" />
    <processor type="Sitecore.XA.Foundation.RenderingVariants.Pipelines.RenderVariantField.RenderModel, Sitecore.XA.Foundation.RenderingVariants" resolve="true" />
    <processor type="Sitecore.XA.Foundation.RenderingVariants.Pipelines.RenderVariantField.RenderToken, Sitecore.XA.Foundation.RenderingVariants" resolve="true" />
    <processor type="Sitecore.XA.Foundation.RenderingVariants.Pipelines.RenderVariantField.RenderTemplate, Sitecore.XA.Foundation.RenderingVariants" resolve="true" />
    <processor type="Sitecore.XA.Foundation.RenderingVariants.Pipelines.RenderVariantField.RenderReference, Sitecore.XA.Foundation.RenderingVariants" resolve="true" />
    <processor type="Sitecore.XA.Foundation.RenderingVariants.Pipelines.RenderVariantField.RenderPlaceholder, Sitecore.XA.Foundation.RenderingVariants" resolve="true" />
    <processor type="Sitecore.XA.Foundation.RenderingVariants.Pipelines.RenderVariantField.RenderQuery, Sitecore.XA.Foundation.RenderingVariants" resolve="true" />
    <processor type="Sitecore.XA.Foundation.RenderingVariants.Pipelines.RenderVariantField.RenderResponsiveImage, Sitecore.XA.Foundation.RenderingVariants" resolve="true" />
</renderVariantField>

After you'll do it you can inject ServiceLocator.ServiceProvider.GetService<IRendering>() in your renderVariantField processor and you will have access to Parameters collectoin (without the need of modifing @Html.RenderingVariants() extension methods)

| improve this answer | |

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.