phphtmlif-statementforeachkirby

Kirby CMS/php: Checking the file type in order to apply varying markup?


Working with the Kirby CMS, I'm trying to pull all the files of a page and apply different markup to images and videos. In Kirby, there is the $file->type() command, which I think I would have to use in an if-statement in order to distuniguish the files, but my current code is not working. What I tried is this:

<?php
$page = page('PageTitle');
$page_files = $page->files();
?>

<div class="slider-container" id="slider#">

    <div class="slider">

        <?php foreach($page_files->sortBy('sort', 'asc') as $page_file): ?>

            <?php if ($page_file->type() == 'image') { ?>

                <div style="background: url('<?= $page_file->url() ?>'); background-size: cover; height: 100vh;"></div>

            <?php endif; ?>

        <?php endforeach ?>

    </div>

</div>

What am I doing wrong?


Solution

  • The answer to my question is a rather obvious one, I made a simple syntactic mistake by using a { instead of : in the first line of the if-statement. This would be the proper, working version of the code, distinguishing between images and videos:

    <?php if($file->type() == 'image'): ?>
      <!-- markup for image -->
    <?php endif ?>
    
    <?php if($file->type() == 'video'): ?>
      <!-- markup for video -->
    <?php endif ?>