How to add a new index to an existing array on Volt? - arrays

So I have an existing array, I want to run a for loop through it and recreate new arrays. I am trying to figure out how to create my own array directly on volt. Here is my code:
{% set oNomesAgendaAmigos = [], oNomesAgendaRecomendado = [], oNomesAgendaAmigosRecomendado = [] %}
{% for oNomeAgenda in oNomesAgenda %}
{% set oNomesAgendasTotal = oNomeAgenda.cliente_nome %}
{% if oNomeAgenda.ind_amigo == 1 %}
{% set oNomesAgendaAmigos = oNomeAgenda %}
{% endif %}
{% if oNomeAgenda.ind_recomendado == 1 %}
{% set oNomesAgendaRecomendado = oNomeAgenda.cliente_nome %}
{% endif %}
{% if oNomeAgenda.ind_recomendado == 1 AND oNomeAgenda.ind_amigo == 1 %}
{% set oNomesAgendaAmigosRecomendado = oNomeAgenda.cliente_nome %}
{% endif %}
{% endfor %}

Last time i have checked there were no mechanism for setting table bit by bit in Volt. The walk-around would be to use array_merge() or implement own filter/method into Volt engine.
Anyway it's a bit against MVC principles. You should set all tables you need over your PHP part of code.
To loop ever array with indexes inside loop you use that trick:
{% for index, value in numbers %}
{{ index }}: {{ value }}
{% endfor %}
I also really appreciate this part of Volt Documentation.

Related

How can I loop an ansible task with script module with jinja code

I have an ansible task where I want to execute script.sh based on condition. My problem is, when I test my task with debug module, the jinja {% for %} for loop works fine, but when i put the jinja code inside script module, the for loop not work. The task execute only the first iteration and stopped.
Anyone can tell me where is the error in my code?
task:
- name: Exec script
script: "{% set tmp_list = [] %}
{% for i in list_a %}
{% set search = namespace(found=False) %}
{% for j in list_b %}
{% if i[l] == j[0] %}
{% set search.found = True %}
{% if i[2] not in j %}
Script1.sh
{% endif %}
{% endif %}
{% endfor %}
{% if not search.found and i[1] not in tmp_list %}
{% do tmp_list.append(i[1]) %}
Script2.sh
{% elif not search.found and i[1] in tmp_list %}
Script1.sh
{% endif %}
{% endfor %}"
args:
executable: /bin/sh

Jekyll - syntax for a for loop in range given by two integer variables

In Python, what I would like to achieve is:
count = 0
for i in range(count, count+5):
count
What is the syntax for this with Jekyll, if it is possible?
{% assign count = 0 %}
{% for i in (...) %}
{{ i }}
{% endfor %}
You can define range with integers {% for i in (1..10) %} or with variables {% for i in (myVar..myOtherVar) %}

Compare Id's of an array in twig

I'm currently comparing two array's and their id's. If they match it should output a count value. It is totally working but I'm thing if there is another way in doing this rather than:
{% for count in product_count %}
{% if count.term_id == child._menu_item_object_id %}
{{ count.count }}
{% endif %}
{% endfor %}
Thanks for any support! :)
Thanks to #DarkBee
{% for count in product_count if count.term_id == child._menu_item_object_id %}

How do I create an array from a forloop?

I have a folder of images that I'd like to render on a page. I'd like these images to be ordered/filtered a particular way. To do that, I understand that the images need to first be together in an array.
I therefore start with an empty array:
{% assign my_array = "" %}
I then loop through the image folder, and attempt different ways of pushing each image into my_array. Example:
{% for image in site.static_files %}
{% if image.path contains "assets/images/target-folder" %}
<!-- Push image into array -->
{{ my_array | push: image }}
{% endif %}
{% endfor %}
Ideally, I can then use this array as intended:
{% for image in my_array | sort:"date" | reverse %}
<!-- show image -->
{% endfor %}
I'm aware that I could make a data file with the images, but I'd like to avoid needing to take that extra step. Thanks for reading.
You are almost there, the way of how you are creating the array it is the only thing to fix.
This {% assign my_array = "" %} creates an empty string. One easy way to create an array in Liquid is to split the above:
{% assign my_array = "" | split: ',' %}
Now you can push items into the array inside a for loop in the following way:
{% for image in site.static_files %}
{% if image.path contains "assets/images/target-folder" %}
<!-- Push image into array -->
{% assign my_array = my_array | push: image %}
{% endif %}
{% endfor %}
Also note that you can do this without a loop using where/where_exp filters:
{% assign my_array = site.static_files |
where_exp: "item", "item.path contains 'assets/images/target-folder'" %}
or:
{% assign target_folder = "assets/images/target-folder" %}
{% assign my_array = site.static_files |
where_exp: "item", "item.path contains target_foler" %}
(Although, unlike the accepted answer, this doesn't precisely correspond to the question's title, it's still a useful option in the described example.)
This solution worked for me:
{% assign namesArr = '' %}
{% for animal in animals %}
{% assign namesArr = namesArr | append: animal.name %}
{% if forloop.last == false %}
{% assign namesArr = namesArr | append: "," %}
{% endif %}
{% endfor %}
{% assign namesArr = namesArr | split: "," %}
Now namesArr is array, we can check array contains some value: https://stackoverflow.com/a/30823063/5638975

Twig - How to loop particular number of times

I need to be able to generate the links certain number of times (stored in int variable)Is there a way to do it out of the box with twig's for loop?
{% for i in numberOfLoops %}
{{ i }}. Some data
{% endfor %}
The above example do not work. I googled it but did not find actual solution. Any support would be very appreciated.
EDIT:
I also tried:
{% set k = 10 %}
{% for i in 0..k %}
{{ i }}
{% endfor %}
but this generates an exception:
com.lyncode.jtwig.exception.ParseException: Wrong binary operation syntax
Explanation: Input position (line 15, pos 27):
{% for i in 0..k %}
^
I found the working example:
{% set k = 10 %}
{% for i in range(1, k) %}
{{ i }}
{% endfor %}
Source: http://twig.sensiolabs.org/doc/templates.html (not very intuitive to find indeed).
I already had a loop in place to iterate over, I solved this for myself with the slice filter.
{% for link in links|slice(0, 12) %}
http://twig.sensiolabs.org/doc/tags/for.html#iterating-over-a-subset
Try this:
{% set k = 10 %}
{% for i in 0..k %}
{{ i }}
{% endfor %}
Documentation: http://twig.sensiolabs.org/doc/tags/for.html

Resources