Anders Kitson
Anders Kitson

Reputation: 1545

How to output settings.html json data into a query in shopify

I am attempting to output a name of a collection from the json data from the customize page of Shopify theme. I can do this fine when its on its own.

For Example:

{{ settings.collection1 }}

However I want it in a query and am not sure how to do this. Where the {{settings.collection1 is I want to output the collection name and have the products be outputted in that collection but currently all that happens is the collection name itself is outputted.

{% for product in collections.{{settings.collection1}}.products %}
   {% capture productLink %}{{ product.url }}{% endcapture %}
   <a href="{{ productLink }}">{{product.title}}</a>
{% endfor %}

Thanks!

Upvotes: 1

Views: 1187

Answers (2)

Steph Sharp
Steph Sharp

Reputation: 11682

{{ ... }} is used for output. You can access a collection with dot notation or square brackets. For example, these 2 lines do the same thing:

collections.frontpage.products
collections['frontpage'].products

If you want to get the collection name from the settings object, the square bracket notation is the one you'll need to use:

collections[settings.collection1].products

Upvotes: 1

Anders Kitson
Anders Kitson

Reputation: 1545

Looked into the templating documentation, turns out I needed use square brackets inside of a tag when using a object.

{% for product in collections.[[settings.collection1]].products %}
   {% capture productLink %}{{ product.url }}{% endcapture %}
   <a href="{{ productLink }}">{{product.title}}</a>
{% endfor %}

Like This

[[settings.collection1]

Upvotes: 1

Related Questions