Exploring Items, Context, And Objects In Sitecore SXA's Scriban
Using A Scriban Template
Start typing to search...
If you've been using SXA you've likely come across the Scriban variant rendering option. It's not the default rendering variant setup as it's less on the GUI drag and drop and more programming, similar to an JavaScript Underscore template. For developers, it's a likely choice due to how it's managed within a single item and it's very similar to view renderings.
What's all available though? It's not immediately obvious what is possible.
How does a Scriban template work though? How do you reference items or objects? Well, once again, it's fairly straightforward. We've layed out the key references below to get you started.
i_home - The home item of the SXA sitei_datasource - The item representing the datasource for the componenti_item - May be identical to i_datasource, however, if the template is part of a Query or Reference rendering variant field, then this of it as a reference field.i_site - The root item of your sitei_page - The item of the page that the template is being rendered uponYou're able to access fields that are on those items a couple ways.
There are two simple ways to access a field's value:
{{ sc_field i_page 'FieldName' }}
{{ i_item.FieldName }}
There are more complex scenarios you can whip up in Scriban that otherwise would require additional .Net code to accomplish, but we'll save those for another time.
Similar to items, you can also access the Sitecore Context within Scriban to determine things like whether the page is being rendered in Experience Editor or not. Those properties are shown below.
o_language - Language of the page being renderedo_model - The model passed into the renderingo_geospatial - Location information of the item being renderedo_context - Context of the itemo_pagemode - Context of the itemWhile it appears o_context and o_pagemode do the same thing, their properties are different.
This object has properties that relate to the system and the user, such as:
is_administratoruserdomaindatabaseThis object is more about the experience allowing you to improve your template based upon in what mode the user is viewing the rendered information.
is_debuggingis_experience_editoris_experience_editor_editingis_normalis_previewis_simulated_device_previewingis_profilingWith these boolean properties you can write simple if statements to alter your Scriban template's result.
{{ if (o_pagemode.is_experience_editor) }}
... Display a message to the author
{{ else }}
... show something else
{{ end }}
This is truly just scratching the surface of what's possible in a Scriban template. Building a more complex component is possible, and surprisingly, requiring less code to accomplish. We just have to explore what other features, functions and options we have at our disposal.