2

Is it possible to start a matrix loop from a particular block ID?

I am trying to set a new block called "New Page" so that an author can split an article into multiple pages.

Currently it's able to detect a page break and exit out of the block field loop. I have also identified the next block's ID.

{% set nextBlockAfterPageBreak = contentBlock.getNext().id %}

Now what I'd like to be able to do is to get all blocks from nextBlockAfterPageBreak.

So for example,

{% for contentBlock in matrixField where firstBlock = 237 %}

Does anybody know if this is possible?

4

offset works for matrix blocks the same way it does for entries.

So you can do this to skip the first 12

{# skip the first 12 blocks #}
{% for contentBlock in matrixField.offset(12) %}
  ...
{% endfor %}
2

I managed to solve this by using this...

{% for contentBlock in matrixField.id('>=237') %}

In reality 237 is defined in a segment.

But it occurred to me that the ID of the block isn't necessarily the order of the block within the Matrix - for example if I add a new block and that's given an ID of 200, and I drag it up - it still remains as 200.

ID != Position

So to resolve this I took advantage of loop.index0 to identify the true order of the row. I then wrap the switch statement within a conditional...

{% if loop.index0>=craft.request.getSegment(3) %}

Now I can output only the rows after the page break.

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.