Ad

Twig Error Saying "Expected Tag Name. Got Something Else Instead"

- 1 answer

I am using OctoberCMS and inside one of my layout files, I have put below code.

{% 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 %}  

The code is working fine, I am getting the expected result. But in code tab , I am keep getting this error saying,

Expected tag name. Got something else instead.

enter image description here Can someone suggest me what I am doing wrong here ?

Thanks

Ad

Answer

Eventually I have come up with something.

{% 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 %}  

Here above I have used key and slice .

Used key to check my current index value of an array inside my loop and then I simply check with key == 0 to check my first record inside my loop. And slice is used to get only 4 records out of the list.

Now the error disappeared as well and the code also works well.

Thanks for support.

Ad
source: stackoverflow.com
Ad