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:




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



Start using the extension in your Scriban template as following:



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:




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