Here is the code:
{% for i in (0..5) %}
{% assign product = recommendations.top_related_products_{{i}}.products[0].title %}
{{ product }}
{% endfor %}
This code throws the error Liquid error: Cannot read property '0' of null
I have 6 arrays under recommendation like top_related_products_0...5.
How do I print the title variable which is inside the top_related_products_0..5?
Create a string containing the variable name, then use square bracket notation to access it.
For example:
{% for i in (0..5) %}
{% capture related_products %}top_related_products_{{i}}{% endcapture %}
{{ recommendations[related_products].products.first.title }}
{% endfor %}
Related
I would like to use a Twig value comes from a "for" loop as a key in another for loop.
I have 2 arrays: "tab" dans "stats".
"tab" array has multiple values, with one named "name", which is an other array with keys and values.
I would like to use the "tab.name" value as a loop variable for my stats array.
I tried with no success to "add" the value directly in my variable.
{% for elem in tab %}
{% for data in stats.elem.name %}
------ My code
{% endfor %}
{% endfor %}
With this I don't have any result because Twig search for "stats.elem.name" array, but in fact for example if in a loop elem.name = "intitule", the real array is stats.intitule.
I hope i'm clear enough… :)
Thanks!
this should work
{% for elem in tab %}
{% for data in stats[elem] %}
{{data}}
{% endfor %}
{% endfor %}
https://twigfiddle.com/mzpjh8
You can do this by changing the array reference for stats to the standard square brackets - stats[elem.name]
{% for elem in tab %}
{% for data in stats[elem.name] %}
<!-- code -->
{% endfor %}
{% endfor %}
Here's my code
{% for filter_prefix in filter_prefixes %}
{{ filter_prefix }}
{% endfor %}
There are some values I want to add to the filter_prefixes, example: Type...
Can I do it like this?
{% for filter_prefix in filter_prefixes %}
{% filter_prefix = 'Type' %}
{{ filter_prefix }}
{% endfor %}
To list all the filter_prefixes and added the Type value as well.
Please help me with this.
Thank you!
Just output the value you want. There is no need to add it to the loop since it is just something you output anyway.
Type
{% for filter_prefix in filter_prefixes %}
{{ filter_prefix }}
{% endfor %}
Or use the Shopify Array filters split and join. So you join your existing array filter_prefixes into a string, append Type and then split that back into an array.
I have a number of select box form fields which I need to output in the template with custom markup as a list. To do this I have created an include which gets passed the current form field as an argument.
I can loop over the list of options to create the list I want without any problems. My problem is that above the list of options I need to display either the form label, or the current selected value. This is the code I have to do this:
{% if field.value %}
{% for value, option in field.field.choices %}
{% if value == field.value %}
{{ option }}
{% endif %}
{% endfor %}
{% else %}
{{ field.label }}
{% endif %}
If for example value in the above code is 1, and field.value is also 1, the equality test doesn't work and the bit of code within the if statement does not get executed. I guessed it might be down to the data type of the two values I was comparing, so I tried casting both to an integer as follows:
{% if field.value %}
{% for value, option in field.field.choices %}
{% if value|add:"0" == field.value|add:"0" %}
{{ option }}
{% endif %}
{% endfor %}
{% else %}
{{ field.label }}
{% endif %}
This worked fine for the case where the values in the select box were integers. However, sometimes my select boxes have booleans as the value, so in this case the comparison won't work with the integer casting applied.
How can I safely compare the two values whatever the type of data is being compared?
I think I have found the answer.
The issue is that the selected value is always a string, and the value in the forloop could be an integer or a boolean (or potentially other data types). The answer is to cast the value in the forloop to a string:
{% if field.value %}
{% for value, option in field.field.choices %}
{% if value|stringformat:"s" == field.value %}
{{ option }}
{% endif %}
{% endfor %}
{% else %}
{{ field.label }}
{% endif %}
I'm looking to hide content if the metafields are empty for a product, but right now it's returning it for all pages which means my if statement is broken somewhere.
Product Page
{% if product.metafields.review %}
{% include 'extra-review' %}
{% else %}
{% endif %}
Review Snippet Page (extra-review.liquid)
{% assign review = product.metafields.review %}
{% assign key = 'author' %}
{% assign key = 'author-img' %}
{% assign key = 'long' %}
<p> Hello world </p>
Any help would be brilliant
EDIT
Added review metafields layout
To check if a namespace exists you can do a comparison against blank. For example:
{% if product.metafields.review != blank %}
...
{% endif %}
You could also used the size if you wanted to ensure you had three keys. Here we simply output the size:
{{ product.metafields.review.size }}
More info on truthy/falsy can be found in the Shopify docs:
https://help.shopify.com/themes/liquid/basics/true-and-false
Truthiness in Liquid is not like Javascript. I've been bitten by this a few times:
Your test should be:
{% if product.metafields.review == true %}
...
{% endif %}
and review in product.metafields.review is the namespace of the review metafields. see https://help.shopify.com/themes/liquid/objects/metafield
i want to do something like this in Shopify:
{% for i in (0..10) %}
{% capture slide %}slide{{i}}{% endcapture %}
{{ settings.slide }}//i need the value of this one
// i want to get the values for settings.slide1, settings.slide2 etc
{% endfor %}
Another example:
{% for i in (0..10) %}
{{ settings.slide[i] }}//i need the value of this one
{% endfor %}
This is a simplified version of what im trying to achieve.
Thanks
Try this:
{% for i in (0..10) %}
{% assign current_slide = 'slide' | append: i %}
{{ settings[current_slide] }}
{% endfor %}