<p>If you're feeling real adventurous you can do something like this: </p>

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

<p>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) </p>

CC BY-SA 4.0
2018-09-21T20:22:02.500Z
2018-09-21T20:22:02.500Z
Matthew Rotter
<p>I wrote a switch condition in my for loop and everything went well. My bad!</p>

CC BY-SA 4.0
2018-09-21T12:59:47.510Z
2018-09-21T12:59:47.510Z
Evolis
<p>I'm aware the answer has been given already , but I just wanted to point out that the <a href="https://docs.craftcms.com/v3/" rel="nofollow noreferrer">Craft documentation</a> has a nice code sample on how to handle this as well. </p>

<p>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. </p>

<p>The relevant part of the documentation can be found <a href="https://docs.craftcms.com/v3/matrix-fields.html#templating" rel="nofollow noreferrer">at this link</a>.</p>

CC BY-SA 4.0
2018-09-26T09:48:41.607Z
2018-09-26T09:48:41.607Z
mrsmn
<p>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.</p>

<p>Here is how i am currently writing a template.</p>

<ul>
<li>Created a section called "Homepage"</li>
<li>Then I made a matrix block "content blocks"</li>
<li>After that I created 3 Blocks and called them "Image left", "Image right", "Full Text"</li>
<li>Went into my <code>_entry.html</code> and wrote some for loops. Example</li>
</ul>

<pre class="lang-TWIG prettyprint-override"><code>{% for block in entry.contentblocks.type('imageLeft') % }
    do something
{% endfor %}
</code></pre>

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

<p>Example:</p>

<p>Backend order:</p>

<ul>
<li>imageLeft</li>
<li>imageRight</li>
<li>imageLeft</li>
</ul>

<p>Frontent view:</p>

<ul>
<li>imageLeft</li>
<li>imageLeft</li>
<li>imageRight</li>
</ul>

<p>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.</p>

<p>How do I prevent this?</p>

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

<templating><craft3><forloop>
Keeping order of blocks like in backend
# Keeping order of blocks like in backend

(Asked by: username_0 on 2018-09-21)

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.

**Tags:** templating, craft3, forloop

---

## 3 answers

### Answer by username_1 on 2018-09-21. Score: 2

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)


---

### Answer by username_0 on 2018-09-21. Score: 1

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


---

### Answer by username_2 on 2018-09-26. Score: 1

I'm aware the answer has been given already , but I just wanted to point out that the [Craft documentation](https://docs.craftcms.com/v3/) 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](https://docs.craftcms.com/v3/matrix-fields.html#templating).


---

