Ad

Specify Variable Array Index For Liquid

- 1 answer

My liquid code is

{% assign _product_id = product.id %}
  {% assign _product_tag = product.collections[0].title}
  {% assign _product_name = product.title %}

  {{_product_tag}}
  {% assign pagla_array = collections[_product_tag].products %}

  {{ pagla_array.first.title }}

Here last line showing nothing. if I use a static index for assigning pagla_array like {% assign pagla_array = collections['Beans'].products %} then it show value. What wrong did I make here?

Ad

Answer

This line:

{% assign _product_tag = product.collections[0].title}

Is not closed correctly. It should end with %}

In addition you should use handles for the collections, not title.

So it should become:

{% assign _product_tag = product.collections[0].handle %}
....
{% assign pagla_array = collections[_product_tag].products %}
Ad
source: stackoverflow.com
Ad