无法打印树枝文件中的键和值

Not able to print the key and values in twig files

我无法在 for 循环中获取键和值 我的要求应该像 标题 - 描述

我无法获取标题和描述中的值

                       (

                        [product_benefit] => Array(
                        [0] => Array
                            (
                                [description] => Lorem Ipsum is simply dummy text of the printing and typesetting industry.
                                [title] => Ipsum Lorem 
                            )
                        [1] => Array
                            (
                                [description] => Lorem Ipsum is simply dummy text of the printing and typesetting industry.
                                [title] => Ipsum Lorem 
                            )

                        [2] => Array
                            (
                                [description] => Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
                                [title] => Ipsum Lorem
                            )
                        [3] => Array
                            (
                                [description] => Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
                                [title] => Entertainment
                            )
                    )


                     {% if product.product_benefit is not null %}
  <ul class="manual-list">
    {% for row in product.product_benefit %}
        {% for slug, item in row %}
                <li><b>{{ item.title - item.description }}</b></li>
        {% endfor %}
    {% endfor %}
   </ul>

您遇到的第一个问题是您的输出现在减去两个字符串,切换到 {{ foo }} - {{ bar}}{{ foo ~'-'~ bar }}

读出数据:

  • 如果您想在代码中直接使用 keys,那么您的第二个 for 已过时。
{% for row in product.product_description %}
    {{ row.title }} - {{ row.description }}
{% endfor %}
  • 如果你想让键保持动态,你需要第二个 for 循环,但你不再使用文字
{% for row in product.product_description %}
    {% for key, value in row %}
        {{ key }} = {{ value }}
    {% endfor %}
{% endfor %}

demo