Sunday, 15 March 2020

Sitecore Scriban Extension - How To Control a Link Field!

In this post I will show you a couple of custom Scriban extensions that I needed to create on a recent project I was working on, first example is how you can get an item URL counting for server URL / Site resolving and another example of how you can pull a link of a link field. Before that if you're new to Scriban, take a quick look for this post, I'm sure you will find it useful. 



While working on a call to action variant for the carousel SXA rendering, I need to pull the Url from the data source item link field, I wanted a way to also count for site resolving as I have multisite, or count for internal, external...etc type of links, in addition to control some custom attributes I wanted to add to my anchor tag.

I found that we can build a custom Scriban extension to do that, we can have a backend processor, where you can pass the target item and the field name, and a simple couple of lines of backend code to generate the Url, Following are the steps you can follow to accomplish this:


Create a custom processor that inherit IGenerateScribanContextProcessor, following is the full processor code:

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();
}
}
view raw gistfile1.txt hosted with ❤ by GitHub



 Next, you will need to register your custom processor into the generateScribanContext pipeline, see the following simple configuration patch file:

<sitecore>
<pipelines>
<generateScribanContext>
<processor type="Sitecore.Foundation.SitecoreExtensions.ScribanExtensions.GetLinkUrl, Sitecore.Foundation.SitecoreExtensions" resolve="true" />
</generateScribanContext>
</pipelines>
</sitecore>
view raw gistfile1.txt hosted with ❤ by GitHub


Start using the extension in your Scriban template as following:

{{get_link i_item "PromoLink"}}
view raw gistfile1.txt hosted with ❤ by GitHub


Lets assume that you have one site, simple link that you just need to add a couple of attributes to, following is a simple way you can go with:

{{ sc_field i_item "PromoLink" [["attribute1", "attribute1value"],["attribute2", "attribute2value"]]}}
view raw gistfile1.txt hosted with ❤ by GitHub



Hope the above help, stay around, on the next post will show you how to build a quick custom search results using rendering variants and Scriban.


1 comment:

sri said...

Thanks for sharing this informative content , Great work
Leanpitch provides online training in Enterprise agile coaching during this lockdown period everyone can use it wisely.
Enterprise agile coaching

Post a Comment