2

I created Rendering Variant contains component field

enter image description here,

I want to pass values from variant like "$item.Name" in Rendering parameters for this component and read the value in component 's cshtml.

enter image description here

Is this correct way to pass values from variant to component or this not doable??

0

You can use NVelocity template and pass field's name to a component.

First, create a VariantTemplate and add $item.Name on Template field. You can then use this variant inside a custom component and read its value from .cshtml or you can use the component directly and just select VariantDefinition.

See: https://doc.sitecore.com/developers/sxa/17/sitecore-experience-accelerator/en/create-a-rendering-variant.html

  • I recommend not to use NVelocity for the things can be achieved without it. – Dawid Rutkowski Jul 31 at 13:06
0

You can access Rendering Parameters in the backend code (controller/repository or view) easily in SXA. This is a view for Title rendering:

@model Sitecore.XA.Foundation.Variants.Abstractions.Models.VariantsRenderingModel

<div @Html.Sxa().Component(Model.Rendering.RenderingCssClass ?? "title", Model.Attributes)>
    <div class="component-content">
        @foreach (BaseVariantField variantField in Model.VariantFields)
        {
            @Html.RenderingVariants().RenderVariant(variantField, Model.Item, Model.RenderingWebEditingParams, Model)
        }
    </div>
</div>

VariantsRenderingModel contains a list of variant fields so you can use them right away (and this is what we are doing there). This model class inherits from RenderingModelBase which has plenty of data. One of that is a Rendering object which has Parameters collection. This is a collection of rendering parameters. You can access them on the view like that:

<div>@Model.Rendering.Parameters["MY_CUSTOM_RENDERING_PARAMETER_NAME"]</div>

This is how you are accessing the rendering parameres on the view.

Now... with the $item.Name which you have there. What did you want to achieve by doing this?

  • I Know how to access rendering parameters in view but what i need is passing Name of each item in variant to component or reading item in variant inside component. – shimaa elsayed Jul 31 at 10:57
  • Sorry, but I don't understand... You have all of that data in the backend already. Show me your desired code, maybe this will help me to understand your problem. – Dawid Rutkowski Jul 31 at 11:42

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.