【问题标题】:Not able to print the key and values in twig files无法在树枝文件中打印键和值
【发布时间】:2019-10-10 09:37:43
【问题描述】:

我无法在 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>

【问题讨论】:

  • 不工作的老板
  • {{ item }}
  • 启用获取值

标签: symfony twig symfony4 drupal-8 drupal-modules


【解决方案1】:

你遇到的第一个问题是你的输出现在减去两个字符串,要么切换到{{ 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

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签