sightly - loop elements over the script | Community
Skip to main content
Level 3
April 3, 2023
Solved

sightly - loop elements over the script

  • April 3, 2023
  • 2 replies
  • 1134 views

Hi all,

I would like to generate script element in sightly as shown below and I've sightly list which gives me name & text

 

---- SIGHTLY LOOP---

<sly data-sly-list.page="${testModel.faqs}">

    name: ${page.name}

    text: ${page.text}

</sly>

-------------------------------------

EXPECTED OUTPUT:

<script type="application/ld+json">

    {

      "@context": "https://schema.org",

      "@type": "FAQPage",

      "mainEntity": [{

         {

        "@type": "Question",

        "name": "How long does it take to process a refund?",

        "acceptedAnswer": {

          "@type": "Answer",

          "text": “description goes here

        }

      }, {

        "@type": "Question",

        "name": "What is the policy for late/non-delivery of items ordered online?",

        "acceptedAnswer": {

          "@type": "Answer",

          "text": “description goes here

        }

      }]

    }

    </script>

 

 

Could you please help how can we integrate sightly list inside script ? 

 

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 Siva_Sogalapalli

@sonur1 You can try something like:
1. Write logic in sling Model and generate JSON the way you want.

public String generateJSON(List<Page> faqs) {
JSONArray faqSchemaObject = new JSONArray();
try {
for(Page pagBean : faqs){
JSONObject faqItemObject = new JSONObject();
faqItemObject.put("@type","Question");
faqItemObject.put("name",pageBean.getName());
JSONObject answer = new JSONObject();
answer.put("@type","Answer");
answer.put("text",pageBean.getText());
faqItemObject.put("acceptedAnswer",answer);
faqSchemaObject.put(faqItemObject);
}
} catch (JSONException e) {
e.printStackTrace();
}
return faqSchemaObject.toString();
}

2. In sightly something like: 

<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": ${ modelobjet.jsonObject }
}

 Hope this should help you. 

2 replies

Shashi_Mulugu
Community Advisor
Community Advisor
April 3, 2023

@sonur1 I don't think you can do this way, as aem is strict xss enabled. What you can do is generate that json object in a sling model and just print a getmethod of sling model in sightly once.

Siva_Sogalapalli
Community Advisor
Siva_SogalapalliCommunity AdvisorAccepted solution
Community Advisor
April 4, 2023

@sonur1 You can try something like:
1. Write logic in sling Model and generate JSON the way you want.

public String generateJSON(List<Page> faqs) {
JSONArray faqSchemaObject = new JSONArray();
try {
for(Page pagBean : faqs){
JSONObject faqItemObject = new JSONObject();
faqItemObject.put("@type","Question");
faqItemObject.put("name",pageBean.getName());
JSONObject answer = new JSONObject();
answer.put("@type","Answer");
answer.put("text",pageBean.getText());
faqItemObject.put("acceptedAnswer",answer);
faqSchemaObject.put(faqItemObject);
}
} catch (JSONException e) {
e.printStackTrace();
}
return faqSchemaObject.toString();
}

2. In sightly something like: 

<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": ${ modelobjet.jsonObject }
}

 Hope this should help you. 

SonuR1Author
Level 3
April 4, 2023

Yes it worked, thanks.

kautuk_sahni
Community Manager
Community Manager
June 7, 2024

@sonur1 We hope you found the AEM community valuable. We anticipate your return as either a learner or a contributor. The community benefits from SMEs like you. Please ask your AEM peers to join and contribute. Happy AEM learning!

Kautuk Sahni