Last active
June 29, 2020 01:02
-
-
Save michaellwest/eec04cae9797290bbe5e9d857dc81af6 to your computer and use it in GitHub Desktop.
Tips for upgrading Sitecore using SXA and Scriban
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# First make your items inherit from /sitecore/templates/System/Geospatial/Coordinate | |
# Leave the existing IPoi there until after Go Live otherwise the data will be lost (or you have to run the import again) | |
# TODO : Update the import script to use the new field | |
# Change to the root ID for items which use the IPoi template | |
# Careers | |
#$rootId = "{B8208D4B-C25E-4381-A183-4177E47A9310}" | |
# POIs | |
$rootId = "{60365D59-F2F2-4C49-91DA-9C7B046178CF}" | |
# Change to the template ID of the items inheriting from IPoi | |
# Job | |
#$filterId = "{0CB1F725-2A60-49D9-8BC4-867F5FDF73FA}" | |
$filterId = "{6EBB38CE-04FC-425C-895B-3C81FA4A7B5C}" | |
$items = Get-ChildItem -Path "master:" -ID $rootId -Recurse | | |
Where-Object { $_.TemplateId -eq $filterId } | |
foreach($item in $items) { | |
$item.Editing.BeginEdit() | |
$item.Fields[[Sitecore.ContentSearch.Utilities.Constants]::Latitude].Value = $item.Fields["{94969C59-C4B8-43ED-9C92-3FBF930C0ACF}"].Value | |
$item.Fields[[Sitecore.ContentSearch.Utilities.Constants]::Longitude].Value = $item.Fields["{8C1AEA63-2E5E-4FEE-B413-D3B1E050DD26}"].Value | |
$item.Editing.EndEdit() > $null | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{ if o_geospatial }} | |
<div class="number-rounder">{{ o_geospatial.distance }} {{ o_geospatial.unit }}</div> | |
{{ end }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LinkTool : Scriban.Runtime.ScriptObject { | |
static [string] GetItemLink([item] $item, [bool] $includeServerUrl = $false) { | |
$managerService = [Sitecore.Abstractions.BaseLinkManager] | |
$linkManager = [Sitecore.DependencyInjection.ServiceLocator]::ServiceProvider.GetService($managerService) -as $managerService | |
if (!$includeServerUrl) { return $linkManager.GetItemUrl($item) } | |
$providerService = [Sitecore.XA.Foundation.Multisite.Services.ILinkProviderService] | |
$linkProviderService = [Sitecore.DependencyInjection.ServiceLocator]::ServiceProvider.GetService($providerService) -as $providerService | |
$linkProvider = $linkProviderService.GetLinkProvider([Sitecore.Context]::Site) | |
$options = $linkProvider.GetDefaultUrlBuilderOptions().Clone() -as [Sitecore.Links.UrlBuilders.ItemUrlBuilderOptions] | |
if($options) { | |
$options.AlwaysIncludeServerUrl = $true | |
} | |
return $linkManager.GetItemUrl($item, $options) | |
} | |
static [string] GetFieldLinkText([item] $item, [string] $fieldName) { | |
$field = [Sitecore.Data.Fields.LinkField]$item.Fields[$fieldName] | |
return $field.Text | |
} | |
} | |
$testTemplate = @" | |
Page: {{ i_page.title }} | |
Item: {{ i_item.title }} | |
Update: {{ sc_field i_page '__Updated' | date.parse | date.to_string '%g' }} | |
{{ sc_linkTool.get_field_link_text i_page 'HeroLink' }} | |
{{ sc_linkTool.get_item_link i_page true }} | |
"@ | |
$item = Get-Item -Path "master:" -ID "{21F2079F-0FD2-4E5C-BA44-2CA5F444F64E}" | |
function Render-ScribanTemplate { | |
[CmdletBinding()] | |
param( | |
[ValidateNotNullOrEmpty()] | |
[string]$ScribanTemplate, | |
[ValidateNotNull()] | |
[item]$PageItem, | |
[item]$ContextItem | |
) | |
$pageContext = New-Object Sitecore.Mvc.Presentation.PageContext | |
$pageContext.Item = $PageItem | |
[Sitecore.Mvc.Common.ContextService]::Get().Push($pageContext) > $null | |
$serviceType = [Sitecore.XA.Foundation.Scriban.Services.IScribanTemplateRenderer] | |
$service = [Sitecore.DependencyInjection.ServiceLocator]::ServiceProvider.GetService($serviceType) -as $serviceType | |
$md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider | |
$utf8 = New-Object -TypeName System.Text.UTF8Encoding | |
$hash = [System.BitConverter]::ToString($md5.ComputeHash($utf8.GetBytes($ScribanTemplate))) | |
$parsedTemplate = $service.Parse($testTemplate, "$($PageItem.ID)+$($hash)+Template") | |
$renderingParams = New-Object Sitecore.XA.Foundation.Mvc.Models.RenderingWebEditingParams | |
$templateContext = $service.GetTemplateContext($false, $renderingParams) | |
$scriptObject = New-Object Scriban.Runtime.ScriptObject | |
$currentItem = $PageItem | |
if($ContextItem) { | |
$currentItem = $ContextItem | |
} | |
$scriptObject.Add("i_item", $currentItem) | |
$tools = New-Object Scriban.Runtime.ScriptObject | |
$tools.Add("sc_linkTool", (New-Object LinkTool)) | |
[Scriban.Runtime.ScriptObjectExtensions]::Import($scriptObject, $tools) | |
$templateContext.PushGlobal($scriptObject) | |
$service.Render($parsedTemplate, $templateContext) | |
} | |
Render-ScribanTemplate -ScribanTemplate $testTemplate -PageItem $item | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div itemscope="" itemtype="http://schema.org/Place"> | |
<div itemprop="geo" itemscope="" itemtype="http://schema.org/GeoCoordinates"> | |
{{ $poi = sc_follow i_page "POI" # This is a local variable made available on the page. }} | |
<meta itemprop="latitude" content="{{ $poi | sc_field "Latitude" }}"><meta itemprop="longitude" content="{{ $poi | sc_field "Longitude" }}"> | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{ $titles = [] }} | |
{{ for i_child in (sc_query i_page "query:./*[@@templatename='Page']") | |
if ( i_child.Title != "") | |
$titles = $titles | array.add (sc_field i_child 'Title') | |
end | |
end}} | |
<ul> | |
{{~ for i_child in $titles | array.sort ~}} | |
<li>{{ i_child }}</li> | |
{{~ end ~}} | |
</ul> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment