1

how can i output my content in pairs?

like this:

div.row >   entry 1 + entry 2
div.row >   entry 3 + entry 4
div.row >   entry 5 + entry 6

so far i have the following

{% for entry in craft.entries.find({section:'events' ,level: 2 }) %}
<div class="row">

    {% if loop.index0 is divisible by(2) %}
    (entry odd output here)
    {% endif %}

    {% if loop.index is divisible by(2) %}
    (entry even output here)
    {% endif %}

</div>
{% endfor %}

but this just gives me a new row for each entry. What's the best approach?

1 Answer 1

3

I would go with Twigs batch filter, which exactly solves your problem.

Here an an adapted example:

{% set items = craft.entries({
    section: 'events',
    level: 2
}).find() %}

{% for row in items|batch(2, 'No item') %}
    <div class="row">
        {% for item in row %}
            {# Your entry #}
        {% endfor %}
    </div>
{% endfor %}
4
  • Thx! Almost there.. Alas this outputs the content of my structure level1 n times. Can't figure out what i'm doing wrong.
    – eek-a-edge
    Commented Sep 30, 2014 at 14:21
  • I changed my example slightly to get the entries you ask for. Not tested, but I think this should work. You could also solve it with chaining {% set items = craft.entries.section('events').level(2).find() %}. Commented Sep 30, 2014 at 14:37
  • No matter how i try to chain it, it keeps giving me the content of the parent. The number of the items is right but not the content. When i wrote {% for entry in craft.entries.find({section:'events' ,level: 2 }) %} the output was fine but then i couldn't do an output in batch.
    – eek-a-edge
    Commented Sep 30, 2014 at 15:03
  • My bad > i never changed my output code to the new function name. Stupid beginners mistake. thank You much for your help!
    – eek-a-edge
    Commented Sep 30, 2014 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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