More Related Content Similar to Scriban for SXA with unlimited potential (20) Scriban for SXA with unlimited potential2. © 2021 Sitecore Corporation A/S. Sitecore® and Own the Experience® are registered trademarks of Sitecore Corporation A/S.
All other brand names are the property of their respective owners.
#SitecoreVDD
#SitecoreVirtualDay
01
02
03
04
05
Rendering variants
Scriban
Context functions
Item members
Using (Sitecore) objects in Scriban
Agenda
3. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
Rendering variants
• The editor can change the look & feel of OOTB (or custom) components
• Change the css – e.g. color
STYLES
• Change the html - e.g. table vs list, extra fields …
VARIANTS
Rendering variants
let you display different appearances of the same rendering
4. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
SXA variants
• Limit # to 10
• Clone component
• Use rules engine
• Provide a preview
• Think of your editors !
Best practices
5. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
SXA Variants
• Try : play with the variants
• Try again (refactor)
• Read the f.. manual (docs)
• Google blogs
• Sitecore Slack & Sitecore StackExchange
How to learn?
6. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
Scriban
• Fast, powerful and lightweight text
templating engine
• Well documented
https://github.com/lunet-io/scriban
• SXA extensions available
https://doc.sitecore.com/developers/sxa/100/sitecore-experience-
accelerator/en/scriban-templates.html
Scriban
7. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
Scriban
• Introduced in v9.3
• Rendering variant child
• Can be used in combination with other types
• (More than) replacing Nvelocity
SXA & Scriban
8. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
SXA & Scriban
• Items / Objects
• Functions
• Extensions on items and fields
• Own customizations possible
Scriban Extensions in SXA
9. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
SXA & Scriban
• i_item i_page i_datasource …
• o_language o_context o_pagemode …
• o_model (external data)
Items / Objects
10. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
SXA & Scriban
• sc_field sc_raw sc_link
• sc_follow sc_followmany sc_query
• sc_parameter (get a rendering parameter)
• …
• sc_execute (executes a rendering variant field located beneath the Scriban template – e.g. Responsive Image)
• sc_translate (gets translation from the SXA dictionary – new in 9.3)
Functions
<h2>{{ sc_follow i_item “Link” | sc_field “Title” }}</h2>
11. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
SXA & Scriban
i_item.
• name display_name language …
• template_name …
• children parent …
• has_layout …
• url
• FieldName (sc_field)
Item extensions
12. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
SXA & Scriban
i_item.Field.
• raw
• target(s) (sc_follow)
Field extensions
<h2>{{ i_item.Link.Title }}<h2>
<p>{{ i_item.Color.Title.raw }}</p>
13. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
SXA & Scriban
• More control over the output
• Compatible with other variant children
Very flexibel
14. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
SXA & Scriban
• Fast
• Lots of features with the SXA extensions
Very powerful
15. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
SXA & Scriban
• Don’t drown yourself
• 1 page ≠ single Scriban template
• Maintainability
• KEEP IT SIMPLE
BUT …
16. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
SXA & Scriban
• Add our own functions
• Add our own item members
• Add our own objects
One step beyond…
17. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
SXA & Scriban - Context function
• IGenerateScribanContextProcessor
• <generateScribanContext> pipeline
https://ggullentops.blogspot.com/2019/11/custom-sxa-scriban-extensions.html
Context Function
18. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
SXA & Scriban - Context function
{{ sc_meow “Key” }}
19. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
SXA & Scriban – Item member
• GetScribanItemMember
• <getScribanItemMembers> pipeline
https://ggullentops.blogspot.com/2019/11/custom-sxa-scriban-extensions.html
Item member
20. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
SXA & Scriban – Item member
{{ i_item.updated }}
{{ i_page.updated }}
…
21. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
SXA & Scriban – Objects
• What if.. we want to show
“Hello {firstname} {lastname}” ?
• Add the User object to Scriban
(as SXA did with the Item object)
Thx @Dawid Rutkowski & Sitecore StackExchange
https://ggullentops.blogspot.com/2020/03/extending-sitecore-scriban-user-object.html
Adding objects
22. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
SXA & Scriban – Objects
Scriban.Runtime.IObjectAccessor
• GetMembers
• TryGetValue
• ToString
1. Object Accessor
23. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
SXA & Scriban – Objects
• Return the names of all properties you want to expose
all value typed properties of a users profile.
GetMembers
24. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
SXA & Scriban – Objects - Object Accessor
• Try to get the value for the given member from the object.
• The bool return value indicates whether the value was found or not
public bool TryGetValue(TemplateContext context, SourceSpan span, object target, string member, out object value)
TryGetValue
25. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
SXA & Scriban – Objects - Object Accessor
26. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
SXA & Scriban – Objects - Object Accessor
27. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
SXA & Scriban – Objects
• Register all accessors
• SitecoreTemplateContext
Sitecore.XA.Foundation.Scriban.ContextExtensions
2. Template Context
28. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
SXA & Scriban – Objects – Template Context
• Override SitecoreTemplateContext
• Constructor: initialize the Object Accessors
• GetMemberAccessorImpl: decide which Accessor to use (based on target type)
• ToString: decide which ToString method to use (based on target type)
Template Context
29. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
SXA & Scriban – Objects – Template Context
30. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
SXA & Scriban – Objects
• Processor to set template context
• generateScribanContext pipeline
• IGenerateScribanContextProcessor
Sitecore.XA.Foundation.Scriban.Pipelines.GenerateScribanContext
3. InitializeScriban Context
31. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
SXA & Scriban – Objects – InitializeScribanContext
InitializeScribanContext
32. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
SXA & Scriban – Objects – InitializeScribanContext
InitializeScribanContext
33. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
SXA & Scriban – Objects
Scriban template:
Usage
<div>
<ul>
{{for i_user in (sc_getusers)}}
<li>{{i_user}} - {{i_user.Email}} - {{i_user.Portrait}}</li>
{{end}}
</ul>
</div>
34. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
What did we learn today?
35. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
What did we learn
Variants are a powerful feature in SXA
Use variants to make the life of your editors easier
Use variants to make the life of your developers easier
Scriban adds even more flexibility and possibilities
Use Scriban – wisely
Write own Scriban extensions if needed
36. © 2021 Sitecore Corporation A/S.
#SitecoreVDD
#SitecoreVirtualDay
What did we learn about Scriban
Custom logic needed create a Context Function
Access to extra (Sitecore) object needed add an Object Accessor
Extra Item property needed create an Item Member
Enjoy unlimited potential !
Keep it simple
38. FOR DISCUSSION PURPOSES ONLY.
Sitecore Confidential and Proprietary. ©2021 Sitecore
Corporation A/S. Sitecore® and Own the Experience® are
registered trademarks of Sitecore Corporation A/S. All other
brand names are the property of their respective owners.
#SitecoreVDD
#SitecoreVirtualDay
Editor's Notes Sc_field needed when space in the fieldname Think about your team members (and yourself) Inject Sitecore context – if needed
Process delegate : our custom logic “sc_meow” (use Func instead of delegate)
Config : resolve = true
Item member = extension method on “item”
Inject Sitecore context – if needed
- Membername
- Resolve function Scriban objects – not related to Sitecore yet Check is target is User
Get properties and return name.. Verify target.
Get the property named "member" from the UserProfile type.
Property is case sensitive!
Get the value of that property from the given object (User) - in our case it's the user.Profile and not the user itself.
This is SXA namespace This is SXA namespace sc_getusers = function to get users (or could be simple function to get the current User) return User object from that function In order of likeliness to use them
Keep your templates simple !!!