{% set counter = 0 %}
{% for remain_todolist in remain_todolists %}
{% if counter == countRem_todolist %}
['{{ remain_todolist.Project_Name }}', {{ remain_todolist.Remaining_Todos }}]
{% else %}
['{{ remain_todolist.Project_Name }}', {{ remain_todolist.Remaining_Todos }}],
{% endif %}~
{% do counter++ %}//Showing Error
{% endfor %}
Volt Increment statement is showing error
"Unknown expression 279"
What i am doing wrong ?
Have you tried without the do keyword?
Anyway, Volt loops already have counters available to use (see Loop Context), here's a version using it:
{% for list in remain_todolists %}
['{{ list.Project_Name }}', {{ list.Remaining_Todos }}]{{ loop.last ? '' : ',' }}
{% endfor %}
As #cvsguimaraes suggest you can use Loop context which should be better in your case.
I don't think that do statement is valid in Volt. If you need counter value outside loop you can increment value just like that:
{% set counter = counter + 1 %}
Related
I'm trying to retrieve the most recent article on a blog. My current code below does not output anything.
{% for article in blogs['myblog'].articles.last %}
{{ article.title }}
{% endfor %}
You don't need a loop in order to access the last item.
{% assign article = blogs['myblog'].articles.last %}
This will set article to the last item. You can then use it as expected.
{{ article.title }}
Docs: https://shopify.dev/docs/themes/liquid/reference/filters/array-filters#last
It can be done like this using forloop.last:
{% for article in blogs['myblog'].articles %}
{% if forloop.last == true %}
{{ article.title }}
{% endif %}
{% endfor %}
This assumes that blogs is a variable. Otherwise, try replacing blogs['myblog'].articles with blog.articles.
I have this:
{% for prop in props %}
{% if prop.status == 'SOLD' %}
{{ name }}
{% endif %}
{% empty %}
<li>No closed deals.</li>
{% endfor %}
and of course it doesn't do what I want, as far as an empty if loop.
Aside from just filtering the statuses into lists from the view, is there an empty for this kind of loop? I tried setting a true/false using a {% with sold=True %}, but it doesn't work that way.
You can do something like below, which would mean if a value is empty, False, or None it will evaluate to true:
{% if not prop.status %}
do something
{% endif %}
OR
use an else statement, which would catch all values that are not 'SOLD'
{% if prop.status == 'SOLD' %}
do stuff
{% else %}
do something else
{% endif %}
I would like to know how how to make a like java in twig
java
i=10;
for(int x=0;x<i;x++){}
I found this on twig:
{% for i in 0..10 %}
* {{ i }} {% endfor %}
But i don't know how to change that 10 to a int variable in twig.
Just place the variable in your for?
{% set i = 10 %}
{% for j in 1..i %}
{{ j }}
{% endfor %}
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 %}
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 %}