Log in Sign up

How to "hard code" the JSON variables into a script tag when I need them in the JavaScript of a page

Following up on this question I would like to find out more in detail how to "hard code the JSON variables into a script tag" when I need to work with them in JavaScript.

For example the tags variable in the page's JSON structure, how can I access this in JavaScript? Do I read it out of the DOM , say, in the load event? Sorry, but I am a bit confused here.


2 years ago, 2 replies   Edit question

Something like this:

{{#entry}}
...
<script>
var author = "{{metadata.author}}";
alert('Author is' + author);
</script>
...
{{/entry}}

Does that make sense? You can even iterate over lists to generate arrays:

<script>
var tags = [ {{#tags}} { tag: "{{tag}}" }, {{/tags}} ];
</script>
Answered 2 years ago · Edit answer

Thanks, David. Much appreciate your reply.

I can see this is essentially defining new variables in JS, not accessing the underlying JSON data, right?

Does this mean there is no way to _modify_the JSON data?

Answered 2 years ago · Edit answer