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 Sitecore Enthusiasts!

Sign up to our bi-weekly newsletter for a bite-sized curation of valuable insight from the Sitecore community.

What’s in it for you?

  • Stay up-to-date with the latest Sitecore news
  • New to Sitecore? Learn tips and tricks to help you navigate this powerful tool
  • Sitecore pro? Expand your skill set and discover troubleshooting tips
  • Browse open careers and opportunities
  • Get a chance to be featured in upcoming editions
  • Learn our secret handshake
  • And more!
Sitecore Snack a newsletter by Fishtank Consulting
 

Meet David Austin

Development Team Lead | Sitecore Technology MVP x 3

📷🕹️👪

David is a decorated Development Team Lead with Sitecore Technology MVP and Coveo MVP awards, as well as Sitecore CDP & Personalize Certified. He's worked in IT for 25 years; everything ranging from Developer to Business Analyst to Group Lead helping manage everything from Intranet and Internet sites to facility management and application support. David is a dedicated family man who loves to spend time with his girls. He's also an avid photographer and loves to explore new places.

Connect with David