I have defined a paragraph type that has a text field and an image field. At the moment the twig template is something like this

<div class='paragraph_wrapper'>
  <div class='text-container'>{{ content.field_text }}</div>
  <div class='image-container'>{{ content.field_image }}</div>

The problem is I want to alternate the positions of the text & image containers for each paragraph. For example the first one would have text then the image, the next would have the image then the text and so on.

How do I get the paragraph template to be aware of its order in the list? If I use a preprocess function how do I get the order than pass the value as a variable to Twig?

  • rather than adding the classes in the paragraphs template, create a template file for each field and add the classes there.
    – No Sssweat
    Commented May 18, 2017 at 11:55

2 Answers 2


I'm not sure what template you're using, and I'm not sure how your site is setup. But if you have a template that is looping through and displaying these paragraphs, you can add a check in your twig file that checks if the paragraph is even or odd.

{% for paragraph in paragraphs %}
    {% if loop.index is divisibleby(2) %}
        <div class='text-container'>{{ paragraph.field_text }}</div>
        <div class='image-container'>{{ paragraph.field_image }}</div>
    {% else %}
        <div class='image-container'>{{ paragraph.field_image }}</div>
        <div class='text-container'>{{ paragraph.field_text }}</div>
    {% endif %}
{% endfor %}
  • This would work assuming the neither of these fields are set to unlimited values.
    – No Sssweat
    Commented May 18, 2017 at 23:17
  • if that's the case couldn't you just nest it like so {% for text in paragraph.field_text %} ?
    – Amy
    Commented May 19, 2017 at 15:25

I think you can use DraggableViews for sorting content, and then it will be very easy to output without extra logic.

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.