Check for odd or even in array in liquid - jekyll

If there is any way to check for odd or even in array.size, for example?
{% if.collections.size == EVEN %}
{% endif %}

Refer to the Maths Modulo filter:
http://docs.shopify.com/themes/liquid-basics/output#modulo
{% assign value = collections.size | modulo:2 %}
{% if value == 0 %}
even
{% else %}
odd
{% endif %}
Or if you'd prefer a one liner:
{{ collections.size | modulo:2 | plus:1 | pluralize:'even','odd' }}

after some experiments, I found the solution, maybe for anyone will be helpfull
{% assign even = false %}
{% for collection in collections %}
{% capture u %}{% cycle 'odd', 'even' %}{% endcapture %}
{% if u == 'even' %}
{% assign even = true %}
{{u}}
{% endif %}
{% endfor %}
{% if even == true %}
collection-even
{% endif %}

int arr[20] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
cout << "The Even no are : \n";
for (int i = 1; i <= 10; i++) // for start for only i....(even nos)
{
if (i % 2 == 0)
{
cout << i;
cout << " ";
}
}
cout << "\nThe Odd no are : \n";
for (int j = 1; j <= 10; j++) // for start for only j....(odd nos)
{
if (j % 2 != 0)
{
cout << j;
cout << " ";
}
}

Related

Nested loops in jinja2 template

I am templating JSON with jinja2 and trying to iterate through list of lists, using 4 nested loops, and it is failing with message:
"AnsibleError: template error while templating string: expected token ':', got '}'"
As original JSON template is a bit bigger, I miss to paste here simple JSON key:value pairs.
Can you please support me on this. Thanx
This is path of template where main logic is:
{ ....
"panels": [
{% set frame_meter_servers= ["frame-meter10-246-44-20", "frame-meter10-246-45-92", "frame-meter10-246-46-234"] %}
{% set frame_procstat_meter_services= ["frame-meter-clock.service", "frame-meter-web.service"] %}
{% set frame_cpanel_backend_config_cluster_nodes= ["10.246.45.189", "10.246.46.102", "10.246.44.163"] %}
{% set frame_procstat_cpanel_services= ["frame-cpanel-backend"] %}
{% set frame_services_monitoring_hosts= ["frame_cpanel_backend_config_cluster_nodes", "frame_meter_servers"] %}
{% set frame_platform_services= ["frame_procstat_cpanel_services", "frame_procstat_meter_services"] %}
{% set id = namespace(total=1) %}
{% set z = namespace(total=0) %}
{% for node in frame_services_monitoring_hosts %}
{% set nodeloop = loop %}
{% for service in frame_platform_services %}
{% if nodeloop.index == loop.index %}
{
...
"x": 0,
"y": {{ z.total }}
},
"id": {{ id.total }},
"panels": [],
...
},
{% set x = namespace(total=0) %}
{% set y = namespace(total=1) %}
{% for hosts in node %}
{% set x = namespace(total=0) %}
{% for services in service %}
{
...
"x": {{ x.total }},
"y": {{ y.total }}
},
"id": {{ id.total }},
...
},
{% set x.total = 4 + x.total %}
{% set id.total = 1 + id.total %}
{% endfor %}
{% set y.total = 6 + y.total %}
{% endfor %}
{% set y = namespace(total=4) %}
{% for hosts in node %}
{% set x = namespace(total=0) %}
{% for services in service %}
{
...
],
"valueName": "current"
} {% set x.total = 4 + x.total %} {% set id.total = 1 + id.total %} {% if not loop.last %},{% endif %}
{% endfor %}
{% set y.total = 6 + y.total %} {% if not loop.last %},{% endif %}
{% endfor %}
{% if node|length == 2 %} {% set z.total = 14 + z.total %} {% else %} {% set z.total = 20 + z.total %}{% endif %}{% if not loop.last %},{% endif %}{% set id.total = 1 + id.total %}
{% endif %}
{% endfor %}
{% set id.total = 1 + id.total %}{% if not loop.last %},{% endif %}
{% endfor %}
],
...
{% if {{ nodeloop.index }} == {{ loop.index }} %}
That is incorrect syntax; everything inside the {% and %} is conceptually Python; what you want to say is:
{% if nodeloop.index == loop.index %}
I didn't check the rest of that wall of text, but I'm super positive that if was wrong, so let's start there

Generating dynamic strings in jinja2

In jinja2, I want to generate strings that enumerate things, different fruits in this example. I have the following template file. Note the line with "Fruit_str not set" is there fore debugging purposes.
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
{% set fruit_str = "Fruit_str not set" %}
{% for fruit in fruits %}
{% if loop.first %}
{% set fruit_str = fruit %}
{% elif loop.last %}
{% set fruit_str = fruit_str + " and " + fruit + "." %}
{% else %}
{% set fruit_str = fruit_str + ", " + fruit %}
{% endif %}
{% endfor %}
You have {{fruit_str}}
</body>
</html>
which gets this JSON as input
{ "fruits" : ["apples", "oranges", "bananas", "pears"] }
The expected output would be
You have apples, oranges, bananas and pears.
However, the actual output is
You have Fruit_str not set
This indicates that the loop isn't run at all. ´I have read the jinja2 documentation on loops (http://jinja.pocoo.org/docs/2.10/templates/), but I can't find what is wrong in this example. What is wrong here?
In Jinja2 a for loop have its own namespace; therefore the variables you set within the loop is local to the loop, and once outside the loop, the variable of the same name would revert to the one of the outer scope.
You can use the namespace object to get around this issue:
{% set ns = namespace(fruit_str="Fruit_str not set") %}
{% for fruit in fruits %}
{% if loop.first %}
{% set ns.fruit_str = fruit %}
{% elif loop.last %}
{% set ns.fruit_str = ns.fruit_str + " and " + fruit + "." %}
{% else %}
{% set ns.fruit_str = ns.fruit_str + ", " + fruit %}
{% endif %}
{% endfor %}
You have {{ns.fruit_str}}
Please see the documentation of namespace for details.

set value command in jinja2 works in some cases and not in some

The following code displays different values of x in different months. Set command of jinja2 works for flag,x but not for total. Why?
{% set flag = 1 %}
{% set total = 0 %}
{% for date in dates %} //dates is some array
{% if flag == 1 %}
{{x}}
{% set flag= 0 %} // I have used 1+1 also that too works
{% elif "Jul" in date %}
{% set x = x*3 %}
{% if x % 10!=0 %}
{% set x = x - x % 10 %}
{% set total = total + x %}
{% endif %}
{% else %}
{{x}}
{% set total = total + x %}
{% endif %}
{% endfor %}
{{total}}
You have too complex computational logic in the template: it is better to move that logic(or its part) into data-provider component.
For example, in the data-provider component you can compute x for every data and place result into data.x. Then {{dates|sum(attribute='x')}} will display total value.

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

swig-template testing condition with subdocument

I have a json:
var json = [{
a: "asdf",
b: "a",
c: {1:{z:30,x:20,y:50},2:{z:30,x:50,y:30}}
},
{
a: "fdsa",
b: "o",
c: {1:{z:10,x:20,y:50},2:{z:0,x:20,y:30}}
}
]
I want to have a condition to check:
if any item z, x, or y in the c object is greater than 30, show the value for a
Is this possible? I did some research but couldn't find any answers.
Please help! Thanks!
I tried
{% for c,b in json.c %}
Your use case is incredibly complex and probably better done server-side, but here's a way you can do it in swig...
{% for item in json %}
{% set show = false %}
{% for set in item.c %}
{% for k in set %}
{% if k > 30 %}
{% set show = true %}
{% endif %}
{% endfor %}
{% endfor %}
{% if show %}
{{ item.a }}
{% endif %}
{% endfor %}