I am actually building a custom shopify theme using tailwind-css and alpinejs. Pls Look at the code below. I am trying to use the array output of {{product.media}} in the x-data component so I can use it to implement a product page carousel ..
<div
x-data="{
sources: {{ product.media }},
activeImage: null,
prev() {
let index = this.sources.indexOf(this.activeImage);
if (index === 0)
index = this.sources.length;
this.activeImage = this.sources[index - 1];
},
next() {
let index = this.sources.indexOf(this.activeImage);
if (index === this.sources.length - 1)
index = -1;
this.activeImage = this.sources[index + 1];
},
init() {
this.activeImage = this.sources.length > 0 ? this.sources[0] : null
}
}"
class="product__media-wrapper md:col-span-3 "
>
<div class="relative">
<template x-for="media in sources">
<div
x-show="activeImage === media"
x-transition
class="mb-3"
>
{% render 'media', media: media, class: 'flex-shrink-0 snap-start w-8/12 col-span-3 mx-auto' %}
</div>
</template>
<a
@click.prevent="prev"
class="cursor-pointer text-white absolute left-0 top-1/2 -translate-y-1/2"
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-10 w-10"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
stroke-width="2"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M15 19l-7-7 7-7"
/>
</svg>
</a>
<a
@click.prevent="next"
class="cursor-pointer text-white absolute right-0 top-1/2 -translate-y-1/2"
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-10 w-10"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
stroke-width="2"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M9 5l7 7-7 7"
/>
</svg>
</a>
</div>
Notice what I wrote in the x-data component ..
sources: {{ product.media }}
I was expecting to get an array of media attached to sources. I think I am not doing it right. I continue to see the error below
..Undefined object media
theme-checkUndefinedObject
plus it does not work in my theme