Accessing liquid properties by variable

Solved
KevinStartsUp
New Member
3 0 0

Is there a way to access a property by variable like so (assuming I had collections named orange apple and peach)?

{% assign fruits = "orange,apple,peach" | split: ',' %}

{% for fruit in fruits %}
  {% for product in collections[fruit] %}
    {{ product.title }}
  {% endfor %}
{% endfor %}

And beyond that can you even access a specific collection by title like that?
 

0 Likes

This is an accepted solution.

You can use the handle of the collection, ex: https://i.imgur.com/TXybHzk.png
Then you just get it out with collections, ex: collections['accessories']
Hope it helps!

LitExtension - #1 Shopping Cart Migration Expert
If our answer is helpful then please accept as solution!
Are you looking for a solution to migrate to Shopify? Contact us for further assistance.
Try our FREE DEMO now!
KevinStartsUp
New Member
3 0 0

Thanks! I didn't know about handles. Also yes, it did turn out that using a variable as the property name does work if the variable is assigned to the handle like

{% for product in collections[fruit].products %}

etc

 

0 Likes