How can I add rich snippet <script> | Community
Skip to main content
August 21, 2024
Solved

How can I add rich snippet <script>

  • August 21, 2024
  • 1 reply
  • 586 views

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>
 
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by h_kataria

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-supported-since-htl-engine-1422-140 

1 reply

h_kataria
Community Advisor
h_katariaCommunity AdvisorAccepted solution
Community Advisor
August 21, 2024

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-supported-since-htl-engine-1422-140