简体   繁体   中英

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?

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 ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM