Counting the number of elements in array - twig

I am looking to count the number of entries I have in an array in Twig. This is the code I've tried:
{%for nc in notcount%}
{{ nc|length }}
{%endfor%}
This however only produces the length of the string of one of the values in the array.
{{nc}} will produce an output of all the values of the array (there are 2) but I want the output to be just the number 2 (the count) and not all the information in the array.

Just use the length filter on the whole array. It works on more than just strings:
{{ notcount|length }}

This expands on the answer by Denis Bubnov.
I used this to find child values of array elements—namely if there was a anchor field in paragraphs on a Drupal 8 site to build a table of contents.
{% set count = 0 %}
{% for anchor in items %}
{% if anchor.content['#paragraph'].field_anchor_link.0.value %}
{% set count = count + 1 %}
{% endif %}
{% endfor %}
{% if count > 0 %}
--- build the toc here --
{% endif %}

Best practice of getting length is use length filter returns the number of items of a sequence or mapping, or the length of a string. For example: {{ notcount | length }}
But you can calculate count of elements in for loop. For example:
{% set count = 0 %}
{% for nc in notcount %}
{% set count = count + 1 %}
{% endfor %}
{{ count }}
This solution helps if you want to calculate count of elements by condition, for example you have a property name inside object and you want to calculate count of objects with not empty names:
{% set countNotEmpty = 0 %}
{% for nc in notcount if nc.name %}
{% set countNotEmpty = countNotEmpty + 1 %}
{% endfor %}
{{ countNotEmpty }}
Useful links:
length
set
for

{%for nc in notcount%}
{{ loop.index }}
{%endfor%}
loop.index -- The current iteration of the loop.
for reference:https://twig.symfony.com/doc/2.x/tags/for.html

Related

How to calculate the composite's elements and get total of them?

Hi I have a composite that has a QTY field and a TOTAL PRICE field and the table below would calculate and display the subtotal. It works properly when there is only one row of composite, but when I add more items, the subtotal field displays two subtotals instead of one as a whole. I want the subtotal field to display 24 instead of 4 and 20. How can twig solve this implementation? In my SUBTOTAL, I have
{% for item in data.item %}
{% set total_qty = (item.qty)|number_format(2,'.',',') %}
{% set per_price = (item.total)|number_format(2,'.',',') %}
{% set net_cost = (total_qty * per_price )|number_format(2,'.',',') %}
{{ net_cost }}
{% endfor %}
Here is the screenshot to give you better understanding
Don't output the net cost inside the for loop.
First create the sum, then display it after the loop.
Also don't use number_format before the final result.
{% set net_cost = 0 %}
{% for item in data.items %}
{% set net_cost = nest_cost + item.qty * item.total %}
{% endfor %}
{{ net_cost|number_format(2, '.', ',') }}
demo

How can i query entries by a field that are within a matrix?

Each entry has a matrix field with fields including start date, end date, location etc
I need to list and display only the entries after now.
I am able to filter them by
{% set queryEntries = craft.entries({
section: ['events'],
}) %}
{% for entry in queryEntries.all() %}
{% if entry.eventDetails.dateFrom|date("U") > "now"|date("U") %}
{{ entry.title }}
{% endif %}
{% endfor %}
This isn't great though as when the events list grows it won't be very performant and I have entries that I don't want.
Am I am able to filter the queryEntries by entry.eventDetails.dateFrom|date("U") > "now"|date("U") ?

Twig strip textarea and create array

I'm trying to strip some text from a value and afterwards create an array from it with the stripped values.
I'm having troubles to strip the text value.
I can only do this on the frontend since I have no access to the backend (SaaS platform)
In below example value.value (originally a textarea) returns the following text:
[185047078]1x something - Type 1
[415533322]1x something - something
[152890667]1x something 500x500 mm
I want to strip the text so I have [185047078], [415533322], [152890667] left or without the brackets.
Normally in JS you would do something like:
hide_ids = txt.match(/[^\]\[]+(?=\])/g)
However it need to be done in Twig.
Afterwards I want to push the values into an array hide_ids.
{% set hide_ids = [] %}
{% if product.custom %}
{% for custom in product.custom %}
{% if 'Some title' in custom.title %}
{% for value in custom.values %}
{% set hide_this_id = value.value %}
{% if hide_this_id matches '{/[^\]\[]+(?=\])/g}' %}
{% set hide_ids = hide_ids | merge([hide_this_id]) %}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% endif %}
{% set hidden = false %}
{% if id in hide_ids %}
{% set hidden = true %}
{% endif %}
What is the equivalent of match in Twig? I also tried replace but I just can't get that text stripped.
Any help greatly appreciated!
You could go with some simple string functions.
{% for id in ids %}
{{ id | split(']', 2)[0] | replace({'[': '',}) }}
{% endfor %}
demo
split is the explode of twig. This will separate your string in chased based on the ] character. The 2nd parameter (2) ensures there will only be maximum 2 parts in the array.
replace is just str_replace
If you wanted to solve this with regex you would either need to write a function/filter with preg_match or install an extension like this

Craft/Twig How to loop multiple key/value pairs and remove duplicates from that loop?

In Craft CMS, I have child entries where each child has a location with "city" and "country" values assigned to it.
I'm looking to output a list of "City, Country" text but remove any duplicates as two or more children might share the same "city,country" pair.
It's important that I can reference the city and country value for each child separately, because I need to use the country value for displaying a flag for each child item in the list.
I've learned about and tried my hand at "twig hash" and "associative arrays" and found usable snippets but can't make it work together for my case.
This does not work:
{% set children = entry.children %}
{% set locations = {} %}
{% for child in children %}
{% set city = child.location.parts.locality %}
{% set country = child.location.parts.country %}
{% if city not in locations %}
{% set locations = locations|merge({ city : country }) %}
{% endif %}
{% endfor %}
{% for location in locations %}
{% for k, v in location %}
{{ k }}, {{ v }} <br />
{% endfor %}
{% endfor %}
If you want to let the city be the key of your array you will need to wrap them in parantheses so the variable will get interpreted by twig.
Also you don't need the double for loop, you are building a one dimensional array
{% set locations = {} %}
{% for child in children %}
{% set city = child.city %}
{% set country = child.country %}
{% if city not in locations %}
{% set locations = locations|merge({ (city) : country }) %}
{% endif %}
{% endfor %}
{% for k,v in locations %}
{{ k }}, {{ v }} <br />
{% endfor %}
demo

Twig - array_pop?

I have a multi dimensional array along the lines of
array(2) {
[11]=> array(1) {
["L2J"]=> array(1) {
["VS7"]=> array(2) {
["26 Feb 2015 12:00"]=> array(2) {
["C"]=> string(1) "9"
["D"]=> string(1) "9"
}
["26 Feb 2015 13:00"]=> array(2) {
["C"]=> string(1) "9"
["D"]=> string(1) "6"
}
}
}
}
}
Now I have done some looping and I am now at the point where I have access to the dates.
{% for sid, psuedos in alerts %}
{% for psuedo, flights in psuedos %}
{% for flight, dates in flights %}
{% endfor %}
{% endfor %}
{% endfor %}
Now I am converting some normal PHP code and at this point, I would do
$firstDate = array_pop(array_keys($dates));
Is there any way to do something like this in Twig? I have searched about but cant seem to find anything.
Update
This is my latest effort, can't seem to get it to slice the last array element though
{% set firstDate = [dates|keys]|last|slice(1) %}
There isn't a Twig function that will do exactly what array_pop() does (return the last array element and shorten the array at the same time), but there are ways to do them separately.
Given:
{% set array = [1,2,3,4,5] %}
To get the last element, use Twig's last filter.
{{ array|last }}
{# returns '5' #}
You can remove only the last element with the slice filter like this: slice(0,-1)
{% set array = array|slice(0,-1) %}
{# array = [1,2,3,4] #}
... or the Craft without filter:
{% set arrayLast = array|last %}
{% set array = array|without(arrayLast) %}
{# array = [1,2,3,4] #}
pop last element
{% set array = [1,2,3] %}
{% set value = array|last %}
{{ value }} {# return 3 #}
{% set array = array|slice(start, length - 1) %}
{% for value in array %}
{{ value }} {# return 1,2 #}
{% endfor %}
pop the first element
{% set array = [1,2,3] %}
{% set value = array|first %}
{{ value }} {# return 1 #}
{% set array = array|slice(start + 1, length) %}
{% for value in array %}
{{ value }} {# return 2,3 #}
{% endfor %}
Just as a quick check, have you tried slice(-1)?
Passing a negative number as the first parameter should start at the end of the array and work that many back.

Resources