All Articles

Scriban Extensions - Sitecore Experience Accelerator

Scriban

Scriban is a fast, powerful, safe and lightweight scripting language and engine for .NET, which was primarily developed for text templating with a compatibility mode for parsing liquid templates.

-Scriban github page

Scriban with SXA

When building rendering variants with SXA, Scriban templates are particularly useful. The field renderers, sections, and tags can be used to build rendering variants. However, when we need to add some complex logic to the variants, Scriban templates are used. Refer to the official documentation for Scriban templates with SXA here.

Scriban Extensions

SXA offers several Scriban item and field extensions OOTB. Sometimes, these fall short when we have specific requirements. In the below example, I explain how we can build custom Scriban extensions.

The scenario below is when we need to get the URL for a link field.

GetLinkUrlFunction.cs

using System;
using Scriban.Runtime;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;
using Sitecore.XA.Foundation.Scriban.Pipelines.GenerateScribanContext;

namespace Foundation.Links.ScribanExtensions
{
    public class GetLinkUrlFunction : IGenerateScribanContextProcessor
    {
        private delegate string LinkUrlDelegate(Item item, string linkFieldName);

        public GetLinkUrlFunction()
        {
        }

        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?.Fields[linkFieldName] == null)
                return "#";

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

            try
            {
                if (itemField.TargetItem == null)
                {
                    var linkField = (LinkField)item.Fields[linkFieldName];
                    return linkField.GetFriendlyUrl();
                }

                var url = Sitecore.Links.LinkManager.GetItemUrl(itemField.TargetItem);
                return url;
            }
            catch
            {
                return "#";
            }
        }
    }
}

Foundation.Links.ScribanExtensions.config

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <generateScribanContext>
        <processor type="Foundation.Links.ScribanExtensions, Foundation.Links" resolve="true"/>
      </generateScribanContext>
    </pipelines>
  </sitecore>
</configuration>

After deploying the above code and config files, we can use the Scriban extension as below:

{{ targetLink = get_link i_item "Link"}}

References

  1. Scriban - Github Page - https://github.com/scriban/scriban
  2. Scriban Templates - SXA

Published May 25, 2022

Sitecore MVP Technology 2024-23. Web Developer with rich experience in Sitecore and ASP.NET MVC.