How pass a variable into collections[].products?

Hey!

So I’m making this for loop:

{% for product in collections[‘nameOfCollection’].products %}
Do something…
{% endfor %}

Now, it works perfectly, but what if I don’t want to hard code the ‘nameOfCollection’?

What if instead of that I’d like to put a variable value? How can I do it?

I’ve tried:

{% for product in collections[‘{{myVariable}}’].products %}
{% endfor %}

But it doesn’t work,

Does anyone have an idea of how to do this?

Hi @diego10_01

You can do it like this, don’t need to add ‘{{ }}’

{%- assign collection_handle = 'myVariable' -%}
{% for product in collections[collection_handle].products %}
      {{ product.title }}
{% endfor %}