1

Very new to Craft & Twig. So I get the following error: Impossible to invoke a method ("url") on a integer variable ("0") with the below code:

{% if module.type == "rotator" %}
    <div class="rotator">
        {% for row in module.contents %}
            {% set position = loop.index0 %}
            {% set previous = loop.index -1 %}
            {% set next = loop.index + 1 %}
            <div class="rotator__item active">
                <img src="{{ row.slideImage.first().url}}" alt="slideImage.title">
            </div>
            <div class="switcher">
                <span class="rotator__control left">
                    <a href="{{ previous.url() }}"> ‹</a>
                </span>
                <span class="previous__title">
                    {{ previous.slideTitle }}
                </span>
                <div class="divider">
                </div>
                <span class="content">
                    <p>{{row.slideTitle}}</p>
                    <a href="row.slideImage.first().url">
                        {{row.slideCaption}}
                    </a>
                </span>
                <span class="rotator__control right">
                    {{ next.slideTitle }}
                <span>
                    <a href="{{ next.url() }}"> ›</a>
                </span>
            </div>
        {% endfor %}
    </div>
{% endif %}

So basically I need a 'previous' and 'next' variable that corresponds to the previous row and the next row in the Super Table. Essentially I need a way to call the current row in the Super Table I'm at, and the previous row and next row relative to the row I'm on.

Here's what my set up looks like:

enter image description here

enter image description here

I've tried loop.index0 + 1 and loop.index + 1. But neither work and I know I am calling them in the wrong way.

1

loop.index and loop.index0 will both return integers.

So when you do {% set previous = loop.index - 1 %}, on the first iteratin of that loop, loop.index returns 1, then you subtract 1 from it and previous is now equal to 0.

Later you call previous.url(), which is equal to 0.url() and the error message you get says there is no method named url() for the integer 0.

If you want to access the previous row, then you can probably do something like {% set previousRow = module.contents[previous] %}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.