Reputation: 385
I want to create array and which I am creating in loop but I want that array to be declared out of the loop.This to be done in shopify: I am using:
{% assign productid="" %}
{% for product in collections.frontpage.products %}
{% assign product = product.id | split: ", " %}
{% endfor %}
{{product}} // should return value 3,4,4 but not returning
My explanation is not good But I tried my best to explain .Please can any one help me in this.
Upvotes: 1
Views: 8670
Reputation: 2058
you have 2 ways to get that:
1:
{% assign productids = "" %}
{% for product in collections.frontpage.products %}
{% assign productids = productids | append: product.id | append: ',' %}
{% endfor %}
<p>{{ productids }}</p>
...
2:
{% assign productids = collections.frontpage.products | map: 'id' %}
{% for pid in productids %}
<p>{{ pid }}</p>
{% endfor %}
Upvotes: 5
Reputation: 15377
Are you trying to create an array of product ids?
You could do that like:
{% assign productids = collections.frontpage.products | map: 'id' %}
{{ productids |join: ','}}
Upvotes: 4