Wednesday, November 6, 2019

What's new in Sitecore SXA 9.3 - Scriban

SXA 9.3

ScribanSXA 9.3 is the successor of 1.9 - they took over the version number of Sitecore. As usual, a few new features are released - like the Aggregated Facet Filter and Facets Summary components.  One of the most eye-catching newbies however is the introduction of Scriban.

Scriban

Scriban is a fast, powerful, safe and lightweight text templating language and engine. It is introduced in the rendering variant system, so now you can add a Scriban template there.  The SXA team released a bunch of extension to Scriban to help us. We can now use Sitecore functions and objects inside a Scriban template.

Scriban SXA
All those extensions are documented by Sitecore itself, so I'm not going to repeat that information. But let's have a look at what we can do with this templating engine based upon a few issues I had before that seems to have an easier solution now. Just to have a few examples...

Examples

Data attributes

As described on https://ggullentops.blogspot.com/2018/10/using-tokens-in-sitecore-sxa-variant.html I had an issue to add classes to variants based upon a droplist field.
By using the Scriban variant it seems we can solve this a lot easier:
<div class="{{ sc_follow i_item "Color" | sc_raw "Title" }}"> ... </div>
What are we doing here? We use:
  1. sc_follow to get a target item in the Color field of i_item
  2. i_item to get the current context item (and pass it to sc_follow)
  3. | to pass this item to the next function
  4. sc_raw because to get the raw value of the Title field
Note that in "normal" situations you could use sc_field to get the value of a field - making it editable in the experience editor. But exactly that benefit makes it an issue here and an editable field inside a class parameter actually broke my experience editor. But it's good to have all options open.

And - we can achieve the same outcome with the short notation:
{{ i_item.Color.Title.raw }}

External data

On https://ggullentops.blogspot.com/2019/03/external-model-data-SXA-variant-Sitecore.html I wrote about showing external data with SXA. I mentioned the Model variant item which still exists and we have a Model Iterator variant now as well (since 1.9). This variant will iterate over your external data (with max and skip parameters). With the introduction of the processSearchItems pipeline in 1.9 (adding an object to your search results) this might become even more important.
Focusing on Scriban here, there is also a way to access that information in the Scriban templates using the o_model context object.

Translations - Dictionary

As we were facing text values in our variants (using the "Text" variant) and we did not want our editors to have to edit/translate those texts there, we wrote a custom variant definition to solve this issue. That solution is still valid, but there is an alternative now.  When you create a new site with SXA in 9.3 you will see a new item in your site root:
SXA Dictionary
The (display) name of the item should be "Dictionary - SITENAME".  This is the root item for the new per site Dictionary that was introduced. You can create dictionary entries (and folders) here, just as you can underneath the /system/Dictionary that is already known. That system dictionary is a fallback in case the key is not found in the site dictionary. You can also define a first level fallback dictionary from another site in the instance (to be able to share dictionary entries across multiple sites).

To get a value from this Dictionary inside your variant, you can call the sc_translate function in a Scriban template (pass the key as parameter).

sc_execute

SXA provided a lot of extensions that are very useful and well described - but I want to dive just a little deeper into one: sc_execute. The reason is that this one gives immense opportunities. The function executes a rendering variant field located in the Sitecore tree beneath the Scriban template.

When I got introduced to Scriban & SXA 9.3 I got the example of a responsive image. There is not (yet) a way to show those inside the template, so you could use sc_execute to get them inside the template. That nice.. but we could take it a step further. We also have the Component variant - since v1.8.

When we combine both we can create Scriban templates that contain other variants that actually render other components. Maybe even add a little rules engine sauce to it, and we have a tremendous cocktail that will blow your mind. 

Just to show we can really do a lot with this. A challenge might be to find a good balance with the templates to keep it all maintainable. 

Custom

We can also create our own Scriban extensions - that will be the next blog post.



As a side note.. if you have used NVelocity in your current solution :
Breaking change
NVelocity was removed from the Sitecore system.
Variants based on NVelocity will no longer work.

No comments:

Post a Comment