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:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Next, you will need to register your custom processor into the generateScribanContext pipeline, see the following simple configuration patch file:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<sitecore> | |
<pipelines> | |
<generateScribanContext> | |
<processor type="Sitecore.Foundation.SitecoreExtensions.ScribanExtensions.GetLinkUrl, Sitecore.Foundation.SitecoreExtensions" resolve="true" /> | |
</generateScribanContext> | |
</pipelines> | |
</sitecore> |
Start using the extension in your Scriban template as following:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{get_link i_item "PromoLink"}} |
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:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{ sc_field i_item "PromoLink" [["attribute1", "attribute1value"],["attribute2", "attribute2value"]]}} |
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:
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