Wrap an `if clauses` around a foreach loop

I tried to use the following code

<?php if($page->hasImages()): ?>
  <?php if($page->template() 'activity'): ?>
    <?php $image = $page->images()->sortBy('sort', 'asc')->first()) ?>
  <?php else: ?>
    <?php foreach($page->images()->sortBy('sort', 'asc') as $image): ?>
  <?php endif ?>
    <figure class="w--full">
      <img src="<?php echo $image->url() ?>" width="<?php echo $image->width() ?>" height="<?php echo $image->height() ?>" >
    </figure>
  <?php endforeach ?>
<?php endif ?>

but the if clauses it’s not being read by kirby. I tried to change the template from the parent template (activity) to the child template (event), since images come from the child template, and it works. But I’d need to show the first image of the collection only when I am in the parent template, as a sort of preview.

I stumbled upon this problem only because I am trying to use the same snippet and adapting it for different uses.

For example, I am calling the above snippet in the parent template by doing this

<?php snippet('event--media', array('page' => $event_current)) ?>

where $event_current is a custom filter collection made in the parent template.

Curious if this is possible to do at all or if I am stretching a bit too much what is technically doable.

:smiley:

if $page in the if statement refers to the child page, you want to compare to the template of the child page, otherwise it doesn’t make sense. Apart from that, there is an operator missing in your if statement, should be

<?php if($page->template() == 'activity'): ?>