Hello. So I have some code that I slapped together. How would I remove the [" "] that is wrapped around the objects in the Array. Im just using the Custom Liquid section to create this. Below I’ll add a screen shot of the table that I’m working on and also the Code that I’m using. Thanks for any help or guidance on how to fix this.
Code:
{% for product in collection.products %}
{% endfor %}
| Series | Rod Pieces | Rod Length | Line Rating | Price | Buy |
| - | - | - | - | - | - |
| {{ product.metafields.custom.blank_series }} | {{ product.metafields.custom.rod_piece }} | {{ product.metafields.custom.blank_length }} | {{ product.metafields.custom.line_rating }} | {{ product.price | money_with_currency }} | <br> {% if product.variants.size > 0 %}<br> View product<br> {% endif %}<br> |
Hey @DavidBeuy , per Shopify’s Liquid docs, it seems the best way to print array values is with a for loop.
For example, instead of just printing:
{{ product.metafields.custom.blank_series }}
You would loop through and print each value using something like:
{% for item in product.metafields.custom.blank_series %}
{{ item }}
{% endfor %}
So question. Im assuming that I need to pass something in where {{ item }} is correct? Im assuming “Item” isn’t the correct syntax to print up the array values.
And just to verify this is how it should theoretically look below?
{% for item in product.metafields.custom.blank_series %}
{{ item }}
{% endfor %}
{% for item in product.metafields.custom.rod_piece %}
{{ item }}
{% endfor %}
{% for item in product.metafields.custom.blank_length %}
{{ item }}
{% endfor %}
{% for item in product.metafields.custom.line_rating %}
{{ item }}
{% endfor %}
Thanks for your patience, I’m relatively new to coding so Im learning a lot here. I also didn’t know about the Shopify Dev Docs so I’ll look over that. Just seems daunting when your newer ha. Thanks!
Also, what would you call {{ item }} in this situation? Is that the product of the Array? I have a feeling that there are set term to print the value or its some thing that I need to put in that I made but don’t know what it is like it should be {{ line_rating }} or {{ custom.line_rating}}
Just thinking out loud. Thanks Again.
I think I found my own solution. Or at least it worked for me and my needs.
1st - I needed to add .value to the end of my product metafields inputs to remove the [" "] so that displayed it the way I wanted with out the brackets and the quotes.
2nd - I added {{ | join " | " }} to the end to add the spacing I need between to create a spacing and break in between each of the values in that array.
{% for product in collection.products %}
{% endfor %}
| Series | Rod Pieces | Rod Length | Line Rating | Price | Buy |
| - | - | - | - | - | - |
| {{ product.metafields.custom.blank_series.value }} | {{ product.metafields.custom.rod_piece }} | {{ product.metafields.custom.blank_length.value | join: " | " }} | {{ product.metafields.custom.line_rating.value | join: " | "}} | {{ product.price | money_with_currency }} | <br> {% if product.variants.size > 0 %}<br> View product<br> {% endif %}<br> |
Glad you were able to find a solution that works for you!
To be clear, I believe the example you previously shared should have worked:
{% for item in product.metafields.custom.blank_series %}
{{ item }}
{% endfor %}
When working with a for loop, you essentially assign a variable, in this case “item” which takes on each value from the array one at a time, and then the inside of the loop executes individually for each item, in this case printing the value.
Here’s the Liquid reference for this specifically: https://shopify.github.io/liquid/basics/types/#array