Twig 错误说 "Expected tag name. Got something else instead"

Twig error saying "Expected tag name. Got something else instead"

我使用的是 OctoberCMS,在我的一个布局文件中,我放了下面的代码。

{% set i = 0 %}
                                    {% for photo in single_property.photos %}
                                        {% if i <= 3 %}

                                            {% if i == 0 %}
                                            First Photo : <br />
                                             <img src="{{photo.getPath() }}" height="30%" width="30%">
                                            {% else %} 
                                            Other Photo : <br />
                                            <img src="{{photo.getPath() }}" height="30%" width="30%">
                                            {% endif %} 
                                         <br />
                                        {% endif %}
                                        {% set i = i + 1 %}
                                    {% endfor %}  

代码运行良好,我得到了预期的结果。但是在代码选项卡中,我一直收到此错误消息,

Expected tag name. Got something else instead.

有人可以建议我在这里做错了什么吗?

谢谢

最终我想出了一些办法。

{% for key, photo in single_property.photos|slice(0, 4) %}                                       

                                            {% if key == 0 %}
                                            First Photo : <br />
                                             <img src="{{photo.getPath() }}" height="30%" width="30%">
                                            {% else %} 
                                            Other Photo : <br />
                                            <img src="{{photo.getPath() }}" height="30%" width="30%">
                                            {% endif %} 
                                         <br />


{% endfor %}  

上面我使用了 keyslice

使用 key 检查循环内数组的当前索引值,然后我只需使用 key == 0 检查循环内的第一条记录。 slice 用于从列表中仅获取 4 条记录。

现在错误也消失了,代码也运行良好。

感谢支持。