【问题标题】:Passing an array to an include in twig将数组传递给树枝中的包含
【发布时间】:2016-02-20 03:39:37
【问题描述】:

好像参数没有正确传递...

    {% set items = {
        item: {
            'id': '1',
            'brand': 'client1',
            'description': 'solutions.client1.description' | trans},
        item: {
            'id':  '2',
            'brand': 'client2',
            'description': 'solutions.client2.description' | trans}
    } %}

    {% include 'site/case-excerpt.html.twig'
        with {'id': items.item.id,
              'brand': items.item.brand,
              'description': items.item.description
        }
  %}

然后在site/case-excerpt.html.twig 文件中:

{% include 'site/testimonials/item.html.twig'
     with {'id': id,
           'brand': brand,
           'description': description
          }
%}

site/testimonials/item.html.twig 文件中:

<div class="carousel-item {{ id }}">
    <img src="images/brands/{{ brand }}.jpg">
    <p>
        {{ description }}
    </p>
</div>

预期的输出如下:

<div class="carousel-item 1">
    <img src="images/brands/client1.jpg">
    <p>
        I'm the content of the translation for the first client
    </p>
</div>
<div class="carousel-item 2">
    <img src="images/brands/client2.jpg">
    <p>
        I'm the content of the translation for the second client
    </p>
</div>

我放弃了通过items 手动循环的想法,因为它似乎可以很好地完成,here for example

【问题讨论】:

    标签: php twig twig-extension


    【解决方案1】:

    在您提供的示例中,它们循环遍历对象,并为每个对象包含一个部分。

    您正在使用items.item。 Twig 不知道你想要什么,另外,用相同的键 item 创建一个数组,也行不通

    {% set items = [{
                'id': '1',
                'brand': 'client1',
                'description': 'solutions.client1.description' | trans},
                {
                'id':  '2',
                'brand': 'client2',
                'description': 'solutions.client2.description' | trans}
        ] %}
    

    然后遍历你的项目并包含部分

    {% for item in items %}
        {% include 'site/case-excerpt.html.twig'
                with {'id': item.id,
                      'brand': item.brand,
                      'description': item.description
                }
    {% endfor %}
    

    【讨论】:

    • 感谢@Mazzy,我确信这是向前迈出的一大步,但是我仍然卡住了,因为我有 3 个不同的文件,而不是两个。我想 1. 基本文件。定义数组并将其传递给 case-excerpt.html.twig 文件 2. case-excerpt.html.twig :循环遍历数组以将 item.html.twig 显示为模板 3. item.html.twig 显示当前数组值的内容 它是否有意义你呢?
    • 知道了。我将发布最终结果,谢谢!
    【解决方案2】:

    这是最终的工作代码

    基础文件。数组被定义并传递给包含。

        {% set items = [{
                'id': '1',
                'brand': 'euromaster',
                'description': 'solutions.euromaster.description' | trans},
            {
                'id':  '2',
                'brand': 'logo-havas-voyages',
                'description': 'solutions.havas.description' | trans}
        ] %}
    
        {% include 'site/case-excerpt.html.twig'
            with {'items': items
            }
        %}
    

    然后在site/case-excerpt.html.twig 文件中: 我们在数组上循环并包含部分

    {% for item in items %}
        {% include 'site/testimonials/item.html.twig'
             with {'id': item.id,
                   'brand': item.brand,
                   'description': item.description
                  }
        %}
    {% endfor %}
    

    site/testimonials/item.html.twig 文件中:

    <div class="carousel-item {{ id }}">
        <img src="images/brands/{{ brand }}.jpg">
        <p>
            {{ description }}
        </p>
    </div>
    

    谢谢@Mazzy!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-17
      • 1970-01-01
      • 2015-04-22
      • 1970-01-01
      • 2013-04-05
      • 2021-11-13
      • 2019-02-05
      相关资源
      最近更新 更多