Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Twig 如何访问名称未知的元素_Twig_Drupal 8 - Fatal编程技术网

Twig 如何访问名称未知的元素

Twig 如何访问名称未知的元素,twig,drupal-8,Twig,Drupal 8,我需要显示一个元素的值,但是他的名字并不总是一样的。 在我的“能力”对象中,我有4个字段,比如说字段1、字段2、字段3、字段4,但在其他值的函数中,我只需要显示其中的一个。 我在我的“能力”对象的另一个字段“field_niveau_attendu”(设置为1、2、3或4)中有值 我尝试使用“set”函数 {%set niveau=“field\u descriptif”~competency.field\u niveau\u attendu.value%} {{competency.field

我需要显示一个元素的值,但是他的名字并不总是一样的。 在我的“能力”对象中,我有4个字段,比如说
字段1、字段2、字段3、字段4
,但在其他值的函数中,我只需要显示其中的一个。 我在我的“能力”对象的另一个字段“field_niveau_attendu”(设置为1、2、3或4)中有值

我尝试使用“set”函数

{%set niveau=“field\u descriptif”~competency.field\u niveau\u attendu.value%}

{{competency.field\u competencies\u transverses.entity.niveau.value}


但如果在领域能力领域中只有4个领域(领域1、领域2、领域3、领域4),你可以尝试使用“for”


默认情况下,细枝的可能重复无法循环对象属性
{% set niveau = "field_descriptif_" ~ competence.field_niveau_attendu.value %}
<p class="">{{ competence.field_competences_transverses.entity.niveau.value }}</p>
{% if competence.field_niveau_attendu.value is defined %}
    {% set niveau = competence.field_niveau_attendu.value %}
    {% for descriptif in competence.field_competences_transverses.entity %}
        {% if loop.index == niveau %}
            {{ descriptif.value }}
        {% endif %}
    {% endfor %}
{% endif %}