0

I am trying to get the value from the Rendering Parameters in Scriban in order to render some new logic. Basically, I want to allow the content editors to choose what colour of the button they will have. I have set up the class names for the colours and everything regarding the frontend part. Now, I only need to get the values from the Droplist since I am using that as a rendering parameters type of datasource.

Here is what I mean by this:

enter image description here

This is the component properties for this specific component called Sign up section. When user selects for example Gold from the Droplist i want to change the button colour to gold.

This is datasource: enter image description here

Here is the template for rendering parameters: enter image description here

And here is the Scriban template:

{{
    buttonColor = ""
    if (sc_parameter "Button Color" != null) 
        parameter =  sc_parameter 'Button Color' 
        buttonColor = ".button-new-sign {{parameter}}"
    end
    backgroundColor = sc_parameter 'Background Color'
    icon = sc_parameter 'Icon'
}}



<div class="call-to-action call-to-action-fullwidth call-to-action-signup" style="background-color: {{backgroundColor}};">
            <div class="container">
                <div class="call-to-action-content">
                    <div class="row flex-md-nowrap justify-content-center">
                        <div
                            class="col-12 col-md-auto flex-shrink-1 d-flex flex-column flex-md-row align-items-center justify-content-center">
                            <svg class="call-to-action-icon icon icon-email">
                                <use xlink:href="#icon-email"></use>
                            </svg>
                            <h2 class="call-to-action-title text-center text-md-left">
                                {{sc_field  i_item "Sign Up Text"}}
                            </h2>
                        </div>
                        <div class="col-12 col-md-auto text-center my-auto">
                        {{ if sc_parameter "Button Color" != "" }}
                        <div class="call-to-action-button {{buttonColor}}">
                        {{ sc_beginfield i_item 'Sign Up Link' [['text', ' ']]}}
                        {{ sc_link_text i_item 'Sign Up Link' }}
                        {{ sc_endfield }} 
                        </div>
                        {{else}}
                        <div class="call-to-action-button button button-white">
                            {{ sc_beginfield i_item 'Sign Up Link' [['text', ' ']]}}
                            {{ sc_link_text i_item 'Sign Up Link' }}
                            {{ sc_endfield }} 
                            </div>
                        {{ end }}       
                        </div>
                    </div>
                </div>
            </div>
        </div>

How to get the Value field from the Colors item, or I can not do that since I am using droplist. Does anyone suggest any other way of doing this?

3
  • Why not using the ootb styles feature? doc.sitecore.com/xp/en/developers/sxa/102/… I think this might be easier than writing it all yourself
    – Gatogordo
    Commented Mar 18, 2022 at 11:00
  • I am not using that since I want to only use the class name on button inside the Sign Up Section. The button is not a component inside the Sitecore, so that I can add the styles on it
    – Samke11
    Commented Mar 18, 2022 at 11:03
  • So you are defining a style.. you really should use styling. It would save you a lot of (duplicate) code as well ...
    – Gatogordo
    Commented Mar 18, 2022 at 20:44

2 Answers 2

2

Droplist fields always store their values as the name of the items. You should use Droplink instead of Droplist.

Getting value in Scriban template

<div>This rendering is in the '{{ sc_parameter 'Placeholder' }}' placeholder.<div>

Reference : https://doc.sitecore.com/xp/en/developers/sxa/93/sitecore-experience-accelerator/the-embedded-functions-for-the-scriban-template.html

Code for getting value without Scriban template :

var getValue = RenderingContext.Current.Rendering.Parameters[" name of the field"];
var valueDetails = RenderingContext.Current.ContextItem.Database.GetItem(ID.Parse(Guid.Parse(getValue)));
var finalvalue = valueDetails["Value"];
7
  • Thank you for the answer. Can something similiar be achievable using Scriban? Or should I just use for example .Value since it is the name of the field, when editing in Scriban?
    – Samke11
    Commented Mar 18, 2022 at 11:34
  • Try to use droplink in field and use this code to get the value {{ sc_parameter 'Placeholder' }}
    – Ashish
    Commented Mar 18, 2022 at 11:39
  • This will not work, this only shows the ID of the item
    – Samke11
    Commented Mar 18, 2022 at 11:49
  • have you tried like this {{ sc_parameter 'Button Color' }} after changing in the field type ?
    – Ashish
    Commented Mar 18, 2022 at 11:53
  • I did, and it only showed me item ID of the lets say "Gold" Color, but not the field called Value inside that item
    – Samke11
    Commented Mar 18, 2022 at 11:54
1

I had to implement something similar and I decided to write my own custom Scriban extension.

The code below will get the value from a Droplink field (which is specified in the Scriban), retrieves the selected item and returns the value from the Value field.

 public class ValueHelperFunction : IGenerateScribanContextProcessor
{
    private delegate string ValueHelperDelegate(string field);

    public ValueHelperFunction()
    {
    }
    public void Process(GenerateScribanContextPipelineArgs args)
    {
        args.GlobalScriptObject.Import("sc_getvalue", new ValueHelperDelegate(GetValue));
    }

    private string GetValue(string field)
    {
        if (string.IsNullOrEmpty(Sitecore.Mvc.Presentation.RenderingContext.CurrentOrNull.Rendering.Parameters[field]))
        {
            return string.Empty;
        }

        var itemId = Sitecore.Mvc.Presentation.RenderingContext.CurrentOrNull.Rendering.Parameters[field];

        return Sitecore.Context.Database.GetItem(new Sitecore.Data.ID(itemId))?["Value"] ?? string.Empty;
    }
}

And you should create a patch config.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/" xmlns:role="http://www.sitecore.net/xmlconfig/role/">
<sitecore>       
    <pipelines>
        <generateScribanContext>
            <processor type="Namespace.ValueHelper, Namespace" resolve="true" />            
        </generateScribanContext>
    </pipelines>
</sitecore>

Finally, you can use your new extension in your scriban template:

{{ sc_getvalue 'Button Color' }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.