Imagine you have a structured data input field with 4 rows. Is it possible to define in the foreach loop of the template to only show the data of row 1 and 2? And on another place in the template to show the data of row 3 and 4.
Right now i use two different structured data fields but it would look more clean to combine it.
I have a data field for the number in my blueprint but i wonder if it is possible to use the automatic generated #1#2#3 order number in my template for the “steps” heading?
A structure field can be converted into a structure object, which is a collection. IMHO, that’s easier to deal with than the yaml array solution suggested by @gearsdigital. You can use all collection methods, in this case, using limit() and offset() are useful:
$benefitsA = $page->benefits()->toStructure()->limit(2);
$benefitsB = $page->benefits()->toStructure()->offset(2)->limit(2);
// then loop through them:
foreach ($benefitsA as $item) {
echo $item->heading(); // etc. for the other fields
}
// then the same for $benefitsB
As regards the numbering, you can either use the counter as suggested above by @sara, or:
@texnixe Thats true, toStructure() is more handy! But your example works for a known amount of entries. If entries are added or removed this wouldn’t work without adapting the template, right?
And while we are at it: The chunk() method creates equally sized chunks:
$chunks = $page->benefits()->toStructure()->chunk(2); // this creates chunks of 2, again, it would be possible to create this number depending on total number of elements to get two more or less equally sized chunks.
// first chunk
if ($chunk = $chunks->first()) {
foreach ($chunk as $benefit) {
echo $benefit->heading();
}
}
// second chunk
if ($chunk = $chunks->nth(2)) {
foreach ($chunk as $benefit) {
echo $benefit->heading();
}
}