Foreach loop one time

Somehow the following code produces an error. I’ve searched a lot to come to an answer, but can’t find it anywhere. So I’m guessing I’m doing something that isn’t possible :slight_smile:

If I run the code without the foreach loop, then it works. But I need the foreach loop :slight_smile:

Any thoughts?

<?php $events = $page; ?> <?php foreach($events as $event): ?>
    <h2><?= $event->title()->html() ?></h2>
<?php endforeach ?>

What are you trying to accomplish? I think you’re confusing some elements, because right now you’re looping over the $page object, while I think you’re trying to loop over some events.

On which page are these events situated? I assume you will need something like this

<?php $events = $pages->find('events')->events()->yaml() ?>
<?php foreach($events as $event): ?>
    <h2><?= $event->title()->html() ?></h2>
<?php endforeach ?>

@dreadnip: guess you mean children:

<?php 
$events = $pages->find('events')->children();
foreach($events as $event): ?>
    <h2><?= $event->title()->html() ?></h2>
<?php endforeach ?>

Or are your events stored in a structure field, @Vivo? Then you would have to use something like this, depending on your structure field name:

$events = $pages->find('events')->events()->structure();

I don’t know what OPs event situation is, it could be children pages but it could also be a custom field in his yaml (like the calender plugin does it)

Oh I just saw you already mentioned that, and I did forget the ->structure() call anyway :slight_smile: good catch, edited my first reply to reflect this correction

Yes, but you can’t loop through a field, unless it is a structure/yaml field, and then not without using either yaml()or toStructure()

1 Like

I’m afraid this won’t work either, because if you use yaml(), you get an array and have to get the individual field values via their index.

echo html($event['whatever_the_field_name']);

@dreadnip Yes I would like to loop once over the $page object. But somehow that doesn’t happen.

I have to say, I’m using this loop for other purposes as well, that’s why it’s there for. So it looks a bit silly to loop the $page, but then again, why doesn’t it work?

You can’t loop through a page object, you can only loop through an array or a collection. Could you please be more specific about what you want to achieve? What is the intention of the foreach loop?

Maybe if you post your code, we have less guesswork to do and can help you better?

@texnixe
Ah ok, it just will not work with $page then :slight_smile:

To give a bit more context to what I’m trying to achieve. It’s a snippet that can be placed on all kinds of templates, where the amount of events displayed is set. I also use this snippet on the event page itself, there it should only display the data of the event itself, hence just the $page.

To rephrase my question: What would be a better approach?

FYI: the snippet contains way more event data then just the H2, just to show a clean example

<?php
if(isset($limit)) {
   $events = page('agenda')->children()->visible()->filterBy('date', '>', time());
   $events = $events->limit($limit);
} else {
   $events = $page;
}
?>


<?php foreach($events as $event): ?>
   <h2><?= $event->title()->html() ?></h2>
<?php endforeach ?>

2 Options:

  1. you leave the loop out of the snippet and only use it to display the code for the single event, so that on the events overview page, you call that single event snippet within the loop (but the loop is then part of the template)
<?php
// in your events overview page
foreach($events as $event):
  snippet('single-event', ['page' => $event]);
endforeach ?>
  1. You check if you have a collection or a single page in your snippet and then act on that.

I’d prefer the first option.

Thanks! I will try the first approach. That should work :slight_smile:

Yes it works! Thank you so much! I’ve got to tell you, I spend hours on this problem :smiley:

Although it was more a php problem, thanks for helping out!