1

Fairly new user here. I am kinda confused how to template. I want to rearrange the order of blocks in my backend and show them in the right order in the front.

Here is how i am currently writing a template.

  • Created a section called "Homepage"
  • Then I made a matrix block "content blocks"
  • After that I created 3 Blocks and called them "Image left", "Image right", "Full Text"
  • Went into my _entry.html and wrote some for loops. Example
{% for block in entry.contentblocks.type('imageLeft') % }
    do something
{% endfor %}

Now when I order those fields in my backend it won't show the right order on my frontend.

Example:

Backend order:

  • imageLeft
  • imageRight
  • imageLeft

Frontent view:

  • imageLeft
  • imageLeft
  • imageRight

I know this is happening because the first for loop will check all first and print them one after another before moving to the next for loop.

How do I prevent this?

I seriously hope somebody understands my issue. I am fairly new to Twig and Craft so I take every pointer I can get.

1

I wrote a switch condition in my for loop and everything went well. My bad!

1

If you're feeling real adventurous you can do something like this:

{% for block in entry.contentblocks %}
   {% include "_about-partials/" ~ block.type %}
{% endfor %}

In this instance I'd keep files in a directory named _about-partials with the same name as the block type (_about-partials/imageLeft.html)

0

I'm aware the answer has been given already , but I just wanted to point out that the Craft documentation has a nice code sample on how to handle this as well.

As the other answers already state, it basically boils down to looping over the blocks and using a switch case (or if/else statements) to determine the type and act accordingly.

The relevant part of the documentation can be found at this link.

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.