Last active
October 1, 2020 12:20
-
-
Save gabrielwalt/c835d70e7263f5d0699d to your computer and use it in GitHub Desktop.
Output JSON into markup with Sightly
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
use(function () { | |
var myObj = { | |
foo: "bar", | |
arr: [1,2,3,4,5,6,7,8,9,10] | |
}; | |
return { | |
json: JSON.stringify(myObj) | |
}; | |
}); |
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 data-sly-use.logic="logic.js" | |
data-json="${logic.json}"> | |
... | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I often see inline script elements used to output JSON, but a more elegant way of doing this is to use a data attribute instead. Also, instead of using the template language to create properly formatted JSON, using a JSON API instead is more meaningful and also much safer.
The code above can be executed with the REPL live Sightly evaluation environment:
https://github.com/Adobe-Marketing-Cloud/aem-sightly-repl