Check if pdf file type exists, if yes render list of only pdf files

Hi everyone,

new to php and kirby and tried the whole day to solve this:

I want to

  1. check, if page and/or child page has at least one pdf file
  2. if pdf file exists
  3. render list of pdf files (only pdf files, not image files etc.)
    if no pdf file exists, nothing is rendered.

checked the guide and the reference, but it is not completely working. HTML is rendered, even in absence of any pdf file.

My code is until now is this:

<?php if ($page->hasDocuments()) ?>
  <h2>Downloads</h2>
  <ul>
    <?php foreach($page->documents()->filterBy('extension', 'pdf') as $pdf): ?>
      <li>
        <a href="<?= $pdf->url() ?>">
          <?= $pdf->filename() ?>
          (<?= $pdf->niceSize() ?>)
        </a>
      </li>
    <?php endforeach ?>
  </ul>

Also tried to set

<?php endif; ?>

at the end, but then the debugger returns “syntax error, unexpected token “endif”, expecting end of file”

The

code is rendered every time.

Trying to filter for a file type pdf with

<?php if ($page->hasDocuments()->filterBy('extension', 'pdf') ?>

returns an error message from the debugger "syntax error, unexpected token “;” "

Can someone push me in the right direction?

Thanks for your help!

I think there is a colon missing at the end of the if statement. Try:

<?php if ($page->hasDocuments()): ?>

And with the missing colon in place, you also need the endif at the end.

Note that your condition hasDocuments() will also be true if there are other types of documents than PDF. I’d therefore do it like this:

<?php
$pdfs = $page->documents()->filterBy('extension', 'pdf');
if ($pdfs->isNotEmpty()) : ?>

 <h2>Downloads</h2>
  <ul>
    <?php foreach($pdfs as $pdf): ?>
      <li>
        <a href="<?= $pdf->url() ?>">
          <?= $pdf->filename() ?>
          (<?= $pdf->niceSize() ?>)
        </a>
      </li>
    <?php endforeach ?>
  </ul>
<?php endif ?>

thanks for the hints! And yes, your way of “writing” php ist much more readable. Always learning ;o)
But I could not get my code running, even with the colon and the endif added. It just will not take the colon. Gave up.

You mentioned the hasDocuments() conditon. Checked the kirby reference against documents(). If I understood the reference right, there is no difference between both, except that hasDocuments() returns a boolean, whereas documents() returns the actual documents, which can have many other file types than just pdf.

Again many thanks for your help, code is working ;o)

That’s right, hasDocuments() checks if there are any files of type document (pdf, etc. $page->documents() | Kirby CMS) whereas $page->documents() returns all files of that type. However, if you check if $page->hasDocuments() that condition might return true if you have pdfs, text files and document types, while $page->documents()->filterBy('extension', 'pdf') could still be empty. In that case you would end up with a headline and an empty ul element.

Thanks for the explanation! Very nice support!