Expand my Community achievements bar.

SOLVED

HTL tags issue

Avatar

Level 4
<sly data-sly-use.product="**.**.**.**.**.models.product">


<script type="application/ld+json"> {
"@context": "http://schema.org",
"@type": "Product",
"name": "${product.name }",

<sly data-sly-test="${product.getReviewSummary != 0 && product.getReviewCount != 0}">
"aggregateRating": {

"ratingValue": "${product.getReviewSummary }",
"reviewCount": "${product.getReviewCount }"
}</sly>
}

</script>
</sly>



output:
<script type="application/ld+json"> {
"@context": "http://schema.org",
"@type": "Product",
"name": "jean",

<sly data-sly-test="">
"aggregateRating": {
"ratingValue": "4.0",
"reviewCount": "2"
}

</sly>
}
</script>

issue is in output <sly data-sly-test=""> tag is getting in json  how to avoid it.




1 Accepted Solution

Avatar

Correct answer by
Community Advisor

HI @user96222 

You can construct the ld+json in the Sling Model with business logic and return as String and use 

 

jsonLDStr = GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create().toJson(jsonLDObj);

 

<script type="application/ld+json">
${myObj.jsonLD @ context='scriptComment'}
</script>

 



Arun Patidar

View solution in original post

8 Replies

Avatar

Level 2

Hi @user96222,

We cannot get rid of the sly tag from the script. We had similar issue so we ended up forming the json object in Sling model - 

chavad_0-1731949104796.png

and then used the schema object in HTL -

chavad_1-1731949397741.png

 

Or you can try using javascript to form the json.

Avatar

Community Advisor

@chavad @user96222 Be careful setting "unsafe" context as this disables XSS protection completely.

Avatar

Level 8

You are right. This is a different issue. I misreas. Apologies.

 

Unless you want to wrap the entire script in a data-sly-test twice for both true and false cases, I advice you prepare your json on sling model and provides it in one go to the component ( same as @arunpatidar indicated ).

Avatar

Level 5

Hi @user96222 ,

 

Can you try updating data-sly-test condition on line 2 and try to execute. Check if the issue still persists

 

Hope this helps

 

Thanks

Avatar

Correct answer by
Community Advisor

HI @user96222 

You can construct the ld+json in the Sling Model with business logic and return as String and use 

 

jsonLDStr = GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create().toJson(jsonLDObj);

 

<script type="application/ld+json">
${myObj.jsonLD @ context='scriptComment'}
</script>

 



Arun Patidar

Avatar

Community Advisor

I second this, moving the business logic to back-end(sling models).

HTL is mainly presentation.