may i get index of elder loop in html? - html

I have 2 loops creating table:
{% for row in row_data %} #first loop
{% for col, row_ in zip(column_names, row) %} #second loop
{{row_}}
{% endfor %}
{% endfor %}
And i need to get first loops index. {{loop.index}} obviously returns second loops index.
Appreciate any help!
UPD:
That works for me:
{% set firstIndex = loop.index %}

Set the first loop index as a variable:
{% for row in row_data %} #first loop
{% set firstIndex = loop.index %}
{% for col, row_ in zip(column_names, row) %} #second loop
{{row_}}
{% endfor %}
{% endfor %}

Related

For loop in Shopify not working over number 9

I'm having an issue with a for loop in Shopify. I'm sure it used to work, but I can't get it to work over the number 9 now.
{% assign productTag1 = Availability14 %} (in this example, the product has only 1 tag, which is Availability14)
{% assign avail_stop = false %}
{% for j in (0..15) %}
{% assign check_avail = 'Availability' | append:j %}
{% if productTag1 contains check_avail %}
{% assign avail_stop = true %}
{% capture tag_name %}{{check_avail}}{% endcapture %}
{% break %}
{% endif %}
{% endfor %}
{% if avail_stop %}
{% assign availability = check_avail | remove:'Availability' | plus:0 %}
{% endif %}
At the moment, I'm returning 1, not 14. I imagine it's something to do with the fact 14 includes a 1, but I can't wrap my head around it.
Any help is appreciated.
You have a {% break %} statement in your if. Once the if becomes true it will exit the loop instantly.
If you want to skip the next code you must use {% continue %} not {% break %}.
On my mind this is an issue with conditional operator. As you said, 14 contains 1, so why not simply use strict conditional operator like this:
{% if productTag1 == check_avail %}
{% assign has_stop = true %}
{% break %}
{% endif %}
(or did I miss something?)

Store result of a query in a variable ( jinja)

I am trying to put the result of a query in a variable but it doesn't work.
I am not sure what to do so it returns 0 as expected. Any ideas? I am using dbt and jinja.
With the below code the results_list variable is (Decimal('0'),))
MACRO
{% macro source_freshness(model, column_name) %}
{% set freshness_query %}
SELECT COUNT 0 AS count
{% endset %}
{% set results = run_query(freshness_query) %}
{% if execute %}
{% set results_list = results.columns[0].values() %}
{% else %}
{% set results_list = [] %}
{% endif %}
{{ return(results_list) }}
{% endmacro %}
call in a model:
{% set freshness_query_test = source_freshness(ref('model'),'date') %}
{% if count in freshness_query_test == 0 %}
do this
{% else %}
do that
{% endif %}
Thank you!
thanks for your help with this. I have not been able to find a direct answer but what I have done is to add the macro in a separate model, and then use the call statement logic in the shared answer Hi, how do we define select statement as a variable in dbt?

How to increment the counter inside a Liquid for loop?

I am struggling to figure out how to increment the index variable within a for loop in Liquid/Jekyll. Currently, I have something along the lines of
{% for i in (0..num_posts) %}
{% if i < some_value %}
do_thing
{% else %}
{% endif %}
{% assign i = i|plus:1 %}
{% if i<some_value %}
do_another_thing
{% else %}
{% endif %}
{% endfor %}
The problem is, instead of incrementing i, it leaves i as the same value.
Things I have tried:
Using {% assign i = i|plus:1 %}.
Using {% increment i %}.
Using
{% assign j = i|plus:1 %}
{% assign i = j %}
I can't use the offset command either since the code doesn't always check only 2 if statements in the loop.
Any ideas?
Here i is not the index.
To get the current index use {{ forloop.index }}.
{% if forloop.index < 5 %}
Do something
{% endif %}
To assign your own custom index inside a loop you may use something like:
{% assign i = 0 %}
{% for thing in things %}
{% assign i = i | plus:1 %}
{% endfor %}
Just use
{% increment my_counter %}
Creates a new number variable, and increases its value by one every time it is called. The initial value is 0. Also works with decrement. But just if you only have one simple counter, can't reset and always starts at "0"

How can I loop on all items where a certain field is not empty?

I want to loop over all items in a collection that have a certain field set (not empty). I tried this:
{% assign papers_with_demos=site.data.papers | where:"demo", not blank %}
{% if papers_with_demos.size > 0 %}
<h2>demos</h2>
{% for paper in papers_with_demos %}]
...
{% endfor %}
{% endif %}
but it does not work; all papers are returned.
My goal is that the heading "demos" will be shown, only if there is one or more paper with a demo.
If papers are being returned, you must be using data files. If a value is empty for a particular key in a data file it will return false. So you could check the data key with a forloop like:
{% for paper in site.data.papers %}
{% if paper.demo %}
something
{% endif %}
{% endfor %}
You'll find more info in the Data Files section of the Jekyll documentation.
Updated in response to your reply:
{% for item in site.data.items %}
{% if item.random %}
{% assign random-val = item.random %}
{% if random-val %}
<p>true</p>
{% endif %}
{% endif %}
{% endfor %}
This will return true if inside items.yml, you have - random: true, nothing if empty.

how to calculate remainder of an integer division in jinja2

I'm trying to make a mod in jinja2 but no way.
{% set index = 1 %}
option 1:
{% for .... %}
{% if {{index % 3 == 0}} %}
{% endif %}
{% set index = index + 1 %}
{% endfor %}
option 2:
{% for .... %}
{% if index.index is divisibleby 3 %}
{% endif %}
{% set index = index + 1 %}
{% endfor %}
Any idea?
Thanks
You just need to remove the {{ }} from your first if statement. This code works...
<!-- {% set index = 9 %} -->
{% set index = 10 %}
{% if index % 3 == 0 %}hi
{% endif %}
{% set index = index + 1 %}
Hope this helps!
You can use the batch filter:
{% for item_batched in items|batch(3) %}
{% for item in items_batched %}
{% endfor %}
{% endfor %}
http://jinja.pocoo.org/docs/dev/templates/#batch