Expand my Community achievements bar.

SOLVED

How can I add rich snippet <script>

Avatar

Level 1

Hi everyone,

I'm trying to add a rich snippet schema directly in the header for Google, but I'm running into an issue. My script needs to be populated with backend data, but when I pass the data, it ends up being empty.

Any suggestions on how to resolve this?

 

<template data-sly-template.richschema="${ @ objectWithProperties }">
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "SearchResultsPage",
"name": "${objectWithProperties.title}",
"description": "${objectWithProperties.description}",
"url": "${objectWithProperties.url}"
}
</script>
</template>
 
1 Accepted Solution

Avatar

Correct answer by
Level 8

Try giving a suitable context to the properties https://github.com/adobe/htl-spec/blob/master/SPECIFICATION.md#121-display-context 
Something like 

<template data-sly-template.richschema="${ @ objectWithProperties }">
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "SearchResultsPage",
"name": "${objectWithProperties.title @ context='scriptString'}",
"description": "${objectWithProperties.description @ context='scriptString'}",
"url": "${objectWithProperties.url @ context='scriptString'}"
}
</script>
</template>

jsonString is also supported since htl-engine-1422-140, so you can try that as well, if it works it will be more suitable 
https://sling.apache.org/documentation/bundles/scripting/scripting-htl.html#display-context-supporte... 

View solution in original post

1 Reply

Avatar

Correct answer by
Level 8

Try giving a suitable context to the properties https://github.com/adobe/htl-spec/blob/master/SPECIFICATION.md#121-display-context 
Something like 

<template data-sly-template.richschema="${ @ objectWithProperties }">
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "SearchResultsPage",
"name": "${objectWithProperties.title @ context='scriptString'}",
"description": "${objectWithProperties.description @ context='scriptString'}",
"url": "${objectWithProperties.url @ context='scriptString'}"
}
</script>
</template>

jsonString is also supported since htl-engine-1422-140, so you can try that as well, if it works it will be more suitable 
https://sling.apache.org/documentation/bundles/scripting/scripting-htl.html#display-context-supporte...