英文:
how to get only the number for how many certain keys (starts with) there are in a twig
问题
I'm struggling to find a way to count certain keys in an array so I don't need to type all the keys in a computed_twig element for some calculations.
我正在努力找到一种方法来计算数组中的特定键的数量,以便在计算某些计算时不需要键入所有键。
I'm trying to get how many keys (questions) in the form so I can use the number in another step.
我想知道表单中有多少个键(问题),以便我可以在另一步中使用这个数字。
Questions are keys and each starts with q_
. In this example there are 5 questions (q_1 to q_5).
问题是键,每个键都以q_
开头。在这个示例中有5个问题(q_1到q_5)。
Here is a sample of my form (actual form has many groups of such questions with tens of questions, all starting with q_
then a sequential number e.g. q_1
,... q_32
etc).
这是我的表单示例(实际表单中有许多这样的问题组,有数十个问题,所有问题都以q_
开头,然后是一个连续的数字,例如q_1
,... q_32
等)。
I'm trialing in this twigfiddle
我正在试验中使用 这个 twigfiddle
This is a minimal sample of my webform yaml:
这是我的webform yaml的最小示例:
qp1:
'#type': wizard_page
'#title': 'Part I'
'#open': true
ps1:
'#type': details
'#title': title...
'#required': true
'#attributes':
class:
- qp1_1
q_1:
'#type': radios
'#title': q1.....
'#options':
- 'option 1 with value 0'
- 'option 2 with value 1'
- 'option 3 with value 2'
- 'option 4 with value 3'
'#required': true
q_2:
'#type': radios
'#title': q2....
'#options':
- 'option 1 with value 0'
- 'option 2 with value 1'
- 'option 3 with value 2'
- 'option 4 with value 3'
'#required': true
q_3:
'#type': radios
'#title': q3.....
'#options':
- 'option 1 with value 0'
- 'option 2 with value 1'
- 'option 3 with value 2'
- 'option 4 with value 3'
'#required': true
q_4:
'#type': radios
'#title': q4.......
'#options':
- 'option 1 with value 0'
- 'option 2 with value 1'
- 'option 3 with value 2'
- 'option 4 with value 3'
'#required': true
q_5:
'#type': radios
'#title': q5........
'#options':
- 'option 1 with value 0'
- 'option 2 with value 1'
- 'option 3 with value 2'
- 'option 4 with value 3'
'#required': true
First, I tried this but it returns all the correct keys while I only want the number of how many they are (i.e. 5
in this example):
{% for qs in qp1.ps1|keys %}
{% if qs starts with 'q_' %}
{{ qs }} {# this returns q_1 q_2 q_3 q_4 q_5 #}
{% endif %}
{% endfor %}
Second, this is giving me as many "3"s as there correctly are q_
keys as 33333
but this is not what I want and I don't understand where the 3
came from!
{% for qs in qp1.ps1|keys %}
{% if qs starts with 'q_' %}
{{ qs | length }} {# this returns 3 3 3 3 3 (number "3" 5 times !) #}
{% endif %}
{% endfor %}
英文:
I'm struggling to find a way to count certain keys in an array so I don't need to type all the keys in a computed_twig element for some calculations.
I'm trying to get how many keys (questions) in the form so I can use the number in another step.
Questions are keys and each starts with q_
. In this example there are 5 questions (q_1 to q_5). Here is a sample of my form (actual form has many groups of such questions with tens of questions, all starting with q_
then a sequential number e.g. q_1
,... q_32
etc). I'm trialing in this twigfiddle
This is a minimal sample of my webform yaml:
qp1:
'#type': wizard_page
'#title': 'Part I'
'#open': true
ps1:
'#type': details
'#title': title...
'#required': true
'#attributes':
class:
- qp1_1
q_1:
'#type': radios
'#title': q1.....
'#options':
- 'option 1 with value 0'
- 'option 2 with value 1'
- 'option 3 with value 2'
- 'option 4 with value 3'
'#required': true
q_2:
'#type': radios
'#title': q2....
'#options':
- 'option 1 with value 0'
- 'option 2 with value 1'
- 'option 3 with value 2'
- 'option 4 with value 3'
'#required': true
q_3:
'#type': radios
'#title': q3.....
'#options':
- 'option 1 with value 0'
- 'option 2 with value 1'
- 'option 3 with value 2'
- 'option 4 with value 3'
'#required': true
q_4:
'#type': radios
'#title': q4.......
'#options':
- 'option 1 with value 0'
- 'option 2 with value 1'
- 'option 3 with value 2'
- 'option 4 with value 3'
'#required': true
q_5:
'#type': radios
'#title': q5........
'#options':
- 'option 1 with value 0'
- 'option 2 with value 1'
- 'option 3 with value 2'
- 'option 4 with value 3'
'#required': true
First, I tried this but it returns all the correct keys while I only want the number of how many they are (i.e. 5
in this example):
{% for qs in qp1.ps1|keys %}
{% if qs starts with 'q_' %}
{{ qs }} {# this returns q_1 q_2 q_3 q_4 q_5 #}
{% endif %}
{% endfor %}
Second, this is giving me as many "3"s as there correctly are q_
keys as 33333
but this is not what I want and I don't understand where the 3
came from!
{% for qs in qp1.ps1|keys %}
{% if qs starts with 'q_' %}
{{ qs | length }} {# this returns 3 3 3 3 3 (number "3" 5 times !) #}
{% endif %}
{% endfor %}
答案1
得分: 0
我终于想出了一个简洁的回答适用于我的情况,但我仍然对知道是否有其他解决方案感兴趣,而不需要使用不相关键的数量。以下Twig代码运行得很好。数字 "4" 是元素 ps1
下非 q_
键的数量。
((qp1.ps1)|length)
返回所有键的数量(在此示例中为 9)。我想要计算的键只是以 q_
开头的键,此示例中有 5 个键。因此,我只减去了非 q_
键的数量(#type
,#title
,#required
,#attributes
)如下:
{{ ((qp1.ps1)|length) - 4 }}
我仍然希望有一种不需要使用非相关键数量的方法,尤其是如果使用“以...开始”筛选器,以便学习,而且也避免每次非相关键的数量可能更改时都需要更新Twig。
英文:
I finally figured out a short clean answer for my case but I'm still interested to know any other solution without the need to use the number of non-relevant keys. The below twig worked nicely. the number "4" is the number of the non q_
keys under the element ps1
.
((qp1.ps1)|length)
returns the number of all the keys (9 in this example here). The keys I want to count are only the ones that start with q_
and they are 5 keys in this example form. So I just deducted the number of non q_
keys (#type
, #title
, #required
, #attributes
) as follows:
{{ ((qp1.ps1)|length) - 4 }}
I still prefer a way without having to use the number of non-relevant keys esp. if using the "starts with" filter for the sake of learning and also to avoid having to update the twig every time the number of non-relevant keys may change.
答案2
得分: 0
A more clean solution would be to use the filter filter
:
{{ qp1.ps1|filter((v,k) => k starts with 'q_')|length }}
You can assign the filtered result to a variable if needed be as well, e.g.
{% set questions = qp1.ps1|filter((v,k) => k starts with 'q_') %}
{{ questions | length }}
英文:
A more clean solution would be to use the filter filter
:
{{ qp1.ps1|filter((v,k) => k starts with 'q_')|length }}
You can assign the filtered result to a variable if needed be as well, e.g.
{% set questions = qp1.ps1|filter((v,k) => k starts with 'q_') %}
{{ questions | length }}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论