I have a matrix field that allows the user to add little 'designed' features throughout the page. These are placed in certain positions on the page and so I want to loop through these but because they are dotted about he page, I cannot do a simple, for loop.
Instead I could get all the blocks like this:
{% set nuggets = entry.nuggets
.with([
'nugget:nuggetBody',
'nugget:icon'
])
.all() %}
Then I could place them like so:
{% set nugget = nuggets[0] %}
{% include '_includes/_components/nugget.twig' with {
self: {
size:'xl',
bgColor: nugget.circleColour.color[0].class,
iconPath:nugget.icon[0].path,
body: nugget.nuggetBody[0].bodyPlain
}
} %}
{% set nugget = nuggets[1] %}
{% include '_includes/_components/nugget.twig' with {
self: {
size:'xl',
bgColor: nugget.circleColour.color[0].class,
iconPath:nugget.icon[0].path,
body: nugget.nuggetBody[0].bodyPlain
}
} %}
etc etc
I wondered if there is a better or more streamlined way than this? And what happens if they don't have 'enough' blocks, how to accomodate, if for example, I have placed 6 around the page in the design and code, but they only add 4. I know I could do this...
{% if nuggets|length >= 3 %}
{% set nugget = nuggets[2] %}
{% include '_includes/_components/nugget.twig' with {
self: {
size:'xl',
bgColor: nugget.circleColour.color[0].class,
iconPath:nugget.icon[0].path,
body: nugget.nuggetBody[0].bodyPlain
}
} %}
{% endif %}
etc
But is there a better approach...?