I’ve had a read of the docs and forum but I cant seem to find an effective workflow for Alpine JS + Hugo.
Here’s what I’m trying to achieve.
I have a page in content, ./content/page.html
Inside that page I would like to set up a function with a data object for Alpine eg.
<div x-data="myFunc()" class="my-thing">
<span x-text="key"></span>
</div>
<script>
function myFunc() {
return {
"key" : "value",
"key2" : "value2",
...,
click() { <!-- do something on click --> },
}
</script>
Where I am getting stuck.
I have a huge data object (key/value pairs), let’s say 1000 items, so I dont want to drop them in there, instead I would like to store them in ./data/mydata.json and then call them into the page.
I understand that I can’t do this:
<script>
function myFunc() {
return {
{{ .Site.Data.myjson | jsonify | safeJS }},
click() { <!-- do something on click --> },
}
</script>
Because:
- The curly braces mess things up
- You cant access .Site.Data from content pages.
I assume I need to move this to a shortcode or something, but this is where I am getting lost.
I assume people have faced this issue using Alpine / Vue etc before which is why Im a bit flummoxed that I cant find a solution on the forums.
Hoping someone has the answer. Thanks for reading.