Insights

Exploring Items, Context, And Objects In Sitecore SXA's Scriban

Using A Scriban Template

Exploring Scriban Items, Context & Objects

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.

Items Available

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 site
  • i_datasource - The item representing the datasource for the component
  • i_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 site
  • i_page - The item of the page that the template is being rendered upon

You're able to access fields that are on those items a couple ways.

Accessing Fields Of Items

There are two simple ways to access a field's value:

sc_item

{{ sc_field i_page 'FieldName' }}

As an attribute

{{ 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.

Accessing Context

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 rendered
  • o_model - The model passed into the rendering
  • o_geospatial - Location information of the item being rendered
  • o_context - Context of the item
  • o_pagemode - Context of the item

While it appears o_context and o_pagemode do the same thing, their properties are different.

o_context

This object has properties that relate to the system and the user, such as:

  • is_administrator
  • user
  • domain
  • database

o_pagemode

This 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_debugging
  • is_experience_editor
  • is_experience_editor_editing
  • is_normal
  • is_preview
  • is_simulated_device_previewing
  • is_profiling

With 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.

Hey, Developers!

We're on the look out for talented developers to join our team.

Think you have what it takes?