Twig错误说"预期的标签名称。得到别的东西"

时间:2017-06-21 05:24:09

标签: php twig octobercms

我正在使用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 %}  

代码工作正常,我得到了预期的结果。但在代码选项卡中,我不断收到此错误,

  

预期的标签名称。得到别的东西。

enter image description here 有人可以告诉我这里我做错了吗?

由于

1 个答案:

答案 0 :(得分:0)

最终我想出了一些东西。

{% 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条记录。

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

感谢您的支持。

相关问题