【发布时间】:2021-11-25 11:33:43
【问题描述】:
我需要制作一个不均匀的画廊,用户可以在其中上传无限数量的图像。画廊由无限重复的 7 幅图像组成。
网站主要使用Paragraphs,所以我创建了一个段落库,用户可以直接上传媒体。我设置了三种图像样式:小、高和大。
我还准备了带有样式解决方案的simple CodePen。但现在我正在为 Twig 模板而苦苦挣扎。我尝试了previous discussions 的一些解决方案,但它们要么失败,要么没有生成图像。
{% block content %}
{% for image in content.field_images['#items'] %}
{% if image %}
<div class="gallery">
<div class="gallery-left">
<div class="inner-wrapper">
<div class="inner-left">
{% if loop.first %}
<div class="field__item">
<img src="{{ image|file_uri|image_style('tall') }}" alt="{{ image.alt }}">
</div>
{% endif %}
{% if loop.index == 2 %}
<div class="field__item">
<img src="{{ image|file_uri|image_style('small') }}" alt="{{ image.alt }}">
</div>
{% endif %}
</div>
<div class="inner-right">
{% if loop.index == 3 %}
<div class="field__item">
<img src="{{ image|file_uri|image_style('tall') }}" alt="{{ image.alt }}">
</div>
{% endif %}
{% if loop.index == 4 %}
<div class="field__item">
<img src="{{ image|file_uri|image_style('small') }}" alt="{{ image.alt }}">
</div>
{% endif %}
</div>
</div>
</div>
<div class="gallery-right">
{% if loop.index == 5 %}
<div class="field__item">
<img src="{{ image|file_uri|image_style('small') }}" alt="{{ image.alt }}">
</div>
{% endif %}
{% if loop.index == 6 %}
<div class="field__item">
<img src="{{ image|file_uri|image_style('tall') }}" alt="{{ image.alt }}">
</div>
{% endif %}
</div>
<div class="bottom">
{% if loop.index % 7 == 0 or loop.last %}
<div class="field__item">
<img src="{{ image|file_uri|image_style('big') }}" alt="{{ image.alt }}">
</div>
{% endif %}
</div>
</div>
{% endif %}
{% endfor %}
{% endblock %}
【问题讨论】:
-
你的
{% for %}- 循环不应该是inside<div class="inner-left">吗?现在您正在为每个图像创建一个完整的画廊 div -
这更有意义,@DarkBee,谢谢!在将 {% for %} 循环塞入元素后,初始块中的图像可以正确显示。但是块模式仍然没有被重复。它只是将最后上传的图像粘贴到初始块的底部。
标签: twig