1

I am using Sitecore 9.3 and SXA, I need to render the general link field in a Scriban template but when I reference it using i_item.Link it returns empty.

Is there a specific syntax to render the general link field?

2

There are two ways to accomplish this, use custom Scriban extension, see the following code sample:

public class GetLinkUrl : IGenerateScribanContextProcessor
{
    private readonly IContext context;
    private delegate string LinkUrlDelegate(Item item, string linkFieldName);

    public GetLinkUrl(IContext context)
    {
        this.context = context;
    }

    public void Process(GenerateScribanContextPipelineArgs args)
    {
        var linkTargetUrl = new LinkUrlDelegate(GetLinkURL);
        args.GlobalScriptObject.Import("get_link", (Delegate)linkTargetUrl);
    }

    public string GetLinkURL(Item item, string linkFieldName)
    {
        if (item == null)
            return "#";

        if (item.Fields[linkFieldName] == null)
            return "#";

        LinkField lnkField = (LinkField)item.Fields[linkFieldName];
        if (lnkField == null)
            return "#";

        return lnkField.GetFriendlyUrl();
    }
}

And you add the processor above to the generate Scriban context as below:

<sitecore>
      <pipelines>
        <generateScribanContext>
          <processor type="Sitecore.Foundation.SitecoreExtensions.ScribanExtensions.GetLinkUrl, Sitecore.Foundation.SitecoreExtensions" resolve="true" />
        </generateScribanContext>
      </pipelines>
</sitecore>

Now in your Scriban template you can use it as:

{{get_link i_item "PromoLink"}}

Another way you can control the anchor is to try something like:

{{ sc_field i_item "PromoLink" [["attribute1", "attribute1value"],["attribute2", "attribute2value"]]}}
| 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.