How to render a text link in Scriban?

How to render a text link in Scriban?

With Scriban we have the sc_link object along with the standard field renderer. So there are a few ways to render a standard text link:

How to render a link in Sitecore MVC?

Rendering a General Link in Sitecore MVC is pretty simple using the FieldRenderer: It uses the attributes of the General Link field to create an tag as a text link. But what if you want to use something more complex, like an image as the link or an entire promo card?

What do you need to know about Scriban in Sitecore?

What we really need is a BeginField and EndField options for Scriban. Fortunately, this is Sitecore and as with most things in Sitecore, we can add that! For the processor, we need to add 2 new functions to scriban, sc_beginfield and sc_endfield. sc_endfield is going to be very simple, no parameters will be passed to it.

Why are link field values not accessible in Scriban?

The reason why you cannot access those properties is because they are not know in the Scriban context provided by the SXA extensions. If you have i_item.CardLink in your Scriban template, that will match a Field object. This object has some properties (eg Target), but not the properties from a LinkField. And those are the ones you are requesting.

Which is an example of a scriban rendering?

Scriban is easily extended, fast and powerful. Our first example is to create a new variant rendering for the Promo item. The promo item contains 6 fields: Promotext, promoIcon, PromoLink, PromoText2, PromoText3 and PromoIcon2.

How to access link field values in Scriban template?

If you have i_item.CardLink in your Scriban template, that will match a Field object. This object has some properties (eg Target), but not the properties from a LinkField. And those are the ones you are requesting. There is a solution, but is it not ootb.. and actually there are 2 solutions. Both require custom coding.

What is the beginrenderimpl function in Scriban?

The BeginRenderImpl implementation is mostly a copy of the AddFieldRendererFunction.RenderFieldImpl that adds the sc_field function to Scriban. This uses the item, field and parameters array to create a new FieldRenderer object: