The embedded functions for the Scriban template
This topic describes the Sitecore embedded functions for the Scriban template.
sc_decorate
sc_decorate
Returns component decoration with styles, grid classes, and attributes required by Creative Exchange.
This helper function is an equivalent of the @Html.Sxa().Component() helper function from MVC Razor views. You can use this function to control your markup in the Scriban templates. If you use this function, your Razor view can be almost empty. You can call the rendering variant functionality and embed the sc_decorate helper function to add the styles and grid classes that your user chose in the rendering parameters.
Parameters
|
Type |
Optional |
Example |
Purpose |
|---|---|---|---|
|
|
yes |
|
Adds classes to the component decoration. |
Example
The following example shows the Scriban template you could use for the Promo component, if you modified the Razor view to not provide any markup but fully delegate the rendering process to your variant instead. The template output with this markup is identical to the Default Promo variant. In the first line of the example, you can see how to use the sc_decorate function. You can add other attributes to the tag in the template directly.
The first line will render in the following way for visitors:
To enable class editing and importing styles for Creative Exchange:
sc_editframe and sc_endeditframe
sc_editframe and sc_endeditframe
Renders the edit frame around an area of HTML and lets you specify a set of Experience buttons that help users edit the content within that area.
Parameters
|
Type |
Optional |
Example |
Purpose |
|---|---|---|---|
|
|
no |
|
Item for which the edit frame is rendered, enabling the editing of its fields. |
|
|
no |
|
The name of the Edit Frame folder item under the |
sc_endeditframe contains no parameters and is used to close the area around the wrapped edit frame.
Example
The following example replicates the Default Gallery component rendering variant, including edit frames. For the default setting go to: /sitecore/system/Settings/Foundation/Experience Accelerator/Rendering Variants/Rendering Variants/Gallery/Default
sc_evaluate
sc_evaluate
Evaluates the rendering variant rule stored beneath the Scriban template and returns the evaluated value (true or false) based on the result of the rule execution.
Parameters
|
Type |
Optional |
Example |
Purpose |
|---|---|---|---|
|
|
no |
|
Item for which the rule is evaluated. |
|
|
no |
|
The name of the rendering variant rule (stored under the Scriban template) that is evaluated during the rendering process. |
Example
You can render a personalization rule in your Scriban template by adding a rendering variant field of type Scriban to a rendering variant. For example, create a variant for your Promo component and name it Personalized Promo. Add a rendering variant field of type Scriban and name it Template. If you then add a rendering variant field of type Rule, pasting the following code in the Template rendering variant field will render a personalization rule.
sc_execute
sc_execute
Executes a rendering variant field located beneath the Scriban template.
SXA lets you embed additional rendering variant fields underneath a Scriban template (similar to, for example, the rendering variant section), but unless you call them explicitly with sc_execute they will not get rendered.
When you runsc_execute i_item "Item below template", you tell the templating engine to render the Item below template in the hierarchy, and paste it where the sc_execute function is.
If you use the Component variant under a Scriban item, you cannot use composite or pagination components in the Component. Sitecore recommends that you do not use components with placeholders under Scriban templates either, because these components might not be rendered correctly on the page.
Parameters
|
Type |
Optional |
Example |
Purpose |
|---|---|---|---|
|
|
no |
|
Item in the context where the rendering variant elements are executed. |
|
|
no |
|
The name of the rendering variant field. |
Example
You can render a responsive image renderer in your Scriban template by adding a rendering variant field of type Scriban to a rendering variant. For example, create a variant for your Promo component and name it Responsive Promo. Add a rendering variant field of type Scriban and name it Template. If you then add a rendering variant field of type Responsive image, pasting the following code in the Template rendering variant field will render a responsive image renderer:
sc_field
sc_field
Renders a Sitecore field that allows fall back when a field is empty and allows you to add parameters to tags for, for example, images or links.
Parameters
|
Type |
Optional |
Example |
Purpose |
|---|---|---|---|
|
|
no |
|
Item for which the value of the field is retrieved. |
|
|
no |
|
Name of the field that is rendered. Alternatively, you can provide an array of field names in which case the function checks the list from left to right and renders the first field that is not empty. In Experience Editor, in Edit mode, the first field is always used, even if it is empty. |
|
|
no |
|
Array of name/value arrays of string allowing you to specify additional parameters to field renderers on fields, such as |
Example
For example, use the following sample code to render a field on an item by calling the sc_field function:
Field fallback example
If you want to provide a field for authors to enter a value for, and make sure that field is not empty even if the author did not enter a value, you can use the field fallback functionality provided in the sc_field function:
The templating engine attempts to render 'NavigationTitle' , and if it does not find a value within it, it moves on to the 'Title' field to return the attempt and finally falls back to '__Display Name' if that fails as well.
Formatting and data attributes on link and image fields
When you want to augment the default rendering behavior of the link or image rendering fields, you can provide an additional name/value collection that will be passed to the Sitecore field renderer as the third parameter, as illustrated in the following examples.
Image renderer example
The following example renders an image with height and width set to 50 and an additional data attribute data-purpose:
The code above results in the following HTML rendered on your page:
The following parameters will be used by the field rendered. Any additional parameters will be used as data attributes:
|
Parameter |
Legal Values |
Default Value |
Description |
|---|---|---|---|
|
as |
true |
false |
Allow stretching the image beyond its original size? |
|
bc |
Color names (such as black or red) and HTML hex color codes (such as CE55E2) |
black |
Background color for the border added when an image is stretched beyond its original size (and |
|
db |
Any Sitecore database defined on the site. |
content database of the current site |
The name of the Sitecore to pull the image from. |
|
dmc |
true |
false |
Disable the media cache for this request? If true, the image will always be retrieved from the database, bypassing the media cache. |
|
h |
Any positive integer |
The height of the image. You must include | |
|
la |
Any valid language name |
Retrieve the image from a specific language version of the item. | |
|
mh |
Any positive integer |
Maximum height of the image to display. Scale the image down to this size if necessary. | |
|
mw |
Any positive integer |
Maximum width of the image to display. Scale the image down to this size if necessary. | |
|
sc |
Any positive floating point number using a dot as a decimal point (such as 1.5, which corresponds to 150%) |
Scale factor for the image displayed. You must include | |
|
thn |
true |
false |
Display a thumbnail of the requested file. Useful for images as well as other media types, such as PDF, flash, and so on. |
|
vs |
Any positive integer |
Retrieve the image from a specific version of the item. | |
|
w |
Any positive integer |
The width of the image. You must include |
Link renderer example
Similar to the link field renderer, with the link renderer you can add attributes depending on your needs. The following example uses the PromoLink target item’s title to add an ARIA accessibility label to your links:
The above example can generate the following HTML:
sc_follow
sc_follow
Returns the item that is selected from a field and stores links to items. If the field can contain links to multiple items, the first item in the list is returned. If the field is empty, the function returns a null value.
Parameters
|
Type |
Optional |
Example |
Purpose |
|---|---|---|---|
|
|
no |
|
Item for which the value of the field is retrieved. |
|
|
no |
|
Name of the field that links to items. |
You can also use the shorter i_item.target notation. If you do, be aware that i_item.target does not allow following links for fields that contain special characters, such as ‘.’, ‘,’, white spaces, and so on.
Example
To render more information about a product linked from a Promo component:
sc_followmany
sc_followmany
Returns an array of items selected in a field that can store link to items. If the field is empty, the function returns an empty list.
Parameters
|
Type |
Optional |
Example |
Purpose |
|---|---|---|---|
|
|
no |
|
Item for which the value of the field is retrieved. |
|
|
no |
|
Name of the field that links to items. |
You can use the shorter i_item.targets notation. If you do, be aware that it does not allow following links for fields that contain special characters, such as ‘.’, ‘,’, white spaces, and so on.
Example
To render products related to the current product:
sc_inheritsfrom
Checks if the item (the first parameter) inherits from a template (the second parameter) and returns true or false.
Parameters
|
Type |
Optional |
Example |
Purpose |
|---|---|---|---|
|
|
no |
|
The item in the context of which the inheritance is executed. |
|
|
no |
|
the ID of a template |
Example
sc_medialink
sc_medialink
Returns the Media URL string to the item provided as the parameter. If the item is null the function returns #.
Function parameters
|
Type |
Optional |
Example |
Purpose |
|---|---|---|---|
|
|
no |
|
Media item for which the link is generated. |
You can also use the shorter i_item.media_url notation. If you do, be aware that it does not allow following links for fields that contain special characters, such as ‘.’, ‘,’, white spaces, and so on.
sc_link
sc_link
Returns the URL string to the item provided as the parameter. If the item is null the function returns #.
Parameters
|
Type |
Optional |
Example |
Purpose |
|---|---|---|---|
|
|
no |
|
Item for which the link is generated. |
You can also use the shorter i_item.url notation. If you do, be aware that it does not allow following links for fields that contain special characters, such as ‘.’, ‘,’, white spaces, and so on.
Example
Render a link to the component Data Source using the Display name of the data source item as the text of the link:
sc_parameter
sc_parameter
Retrieves the rendering parameter values.
Parameters
|
Type |
Optional |
Example |
Purpose |
|---|---|---|---|
|
|
no |
|
Name of the parameter for which the value is retrieved. |
sc_placeholder
sc_placeholder
Renders a placeholder with optional context switch.
Parameters
|
Type |
Optional |
Example |
Purpose |
|---|---|---|---|
|
|
no |
|
The placeholder key. |
|
|
yes |
|
Optional item to switch context to. If the item is specified, components inside the placeholder behave as if they were rendered on |
Example 1
The following example shows you how to render a dynamic SXA placeholder:
Example 2
The following example shows you how to render a dynamic SXA placeholder and switch its context to a related i_product item. The components in that placeholder behave as if the page on which they were rendered was i_product.
sc_query
sc_query
Executes a Sitecore query and returns the resulting items as an array.
Parameters
|
Type |
Optional |
Example |
Purpose |
|---|---|---|---|
|
|
no |
|
The item in the context of which the query is executed. |
|
|
no |
|
The query to execute. |
Example
sc_raw
sc_raw
Retrieves the raw value of a field from an item.
Parameters
|
Type |
Optional |
Example |
Purpose |
|---|---|---|---|
|
|
no |
|
Item for which the value of the field is retrieved. |
|
|
no |
|
The name of the field that you want to retrieve. |
Example
sc_search
Executes content search queries.
Parameters
|
Type |
Optional |
Example |
Purpose |
|---|---|---|---|
|
|
no |
|
Item for which the value of the field is retrieved. |
|
|
no |
|
Limit search results to for example, the current page. |
|
|
yes |
|
Enter amount of results to display. |
|
|
yes |
|
Enter amount of results to skip. |
Example
sc_translate
sc_translate
Returns the text according to the input string and the current language the page is rendered in. Translations are stored in a dictionary that is located at the root of your site item.
Parameters
|
Type |
Optional |
Example |
Purpose |
|---|---|---|---|
|
|
no |
|
The key from the site dictionary. |
|
|
yes |
|
Provide the language code to force the text to be translated to the specific language instead of taking the language from the request context. |