【问题标题】:Symfony2 Twig Change Sub-Loop Limit (Batch filter)Symfony2 Twig 更改子循环限制(批处理过滤器)
【发布时间】:2015-09-17 00:12:52
【问题描述】:

首先; http://twig.sensiolabs.org/doc/filters/batch.html

我的问题是,我想根据循环索引显示 x 个图像。

如果批处理循环是奇数,则显示 2 张图像(在子循环中循环两次),否则仅显示 1 张图像。 (在子循环中只循环一次)。

这是我的代码,但它不起作用。

有什么想法吗?

{% set batch_value = 2 %}
{% for row in getImages(gallery, 'Galeri', 'Detay')|slice(0, 5)|batch(batch_value) %}
    <div class="gallery-column {% if loop.index is odd %}gallery-thumbs{% else %}gallery-original{% endif %}">
        {% for image in row|slice(0, loop.index is odd ? 2 : 1) %}
            <div class="image-item">
                <a class="fancybox" rel="gallery" href="{{ asset(image.image) }}">
                    <figure>
                        <img src="{{ asset(image.image) }}">
                    </figure>
                    <div class="text-wrapper">
                        <div class="align-content">
                            <div class="text-content">
                                <i class="fa fa-2x fa-search"></i>
                            </div>
                        </div>
                    </div>
                </a>
                {{ batch_value }}
            </div>
        {% endfor %}

        {% if loop.index is odd %}
            {% set batch_value = 1 %}
        {% else %}
            {% set batch_value = 2 %}
        {% endif %}
    </div>
{% endfor %}

截图如下:

【问题讨论】:

  • 它没有按我的意愿工作。只需为每行打印 2 个项目。我要打印 2、1、2 个项目,共 5 个。

标签: php symfony for-loop twig


【解决方案1】:

我不完全确定您要做什么,但我希望这会有所帮助:

样板代码

{% set images = getImages(gallery, 'Galeri', 'Detay') %}
{% for key, row in images|slice(0, 5)|batch(big == false ? 2 : 1) %}
    {% for image in row %}
        <img src="{{ asset(images[key].image) }}" />
    {% endfor %}
{% endfor %}

当您已经将变量分配给“行”时,我真的不明白为什么还需要内部循环

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多