Display text when no matching search results

Hey,
I have integrated the search function on a page search.php.
If the user searches for something, the matching results are displayed.
But is it possible to display a special text when there are no matching results?

templates > search.php

<form>
   <input type="search" placeholder="Suchbegriff eingeben" name="q" value="<?= html($query) ?>">
   <input type="submit" value="Suche">
</form>

<?php if ($results->isNotEmpty()): ?>
  <h3>Suchergebnisse für: <?= html($query) ?></h3>
<?php endif ?>
  <?php foreach ($results as $result): ?>
    <a href="<?= $result->url() ?>">
      <?= $result->startbild()->toFile() ?>
        <?= $result->title() ?>
    </a>
  <?php endforeach ?>

controllers > search.php

<?php
return function ($site) {
  $query   = get('q');
  $results = $site->index()->filterBy('template', 'not in', ['search'])->search($query, ['words' => true]);

  return [
    'query'   => $query,
    'results' => $results,
  ];
};

Thank you

<form>
   <input type="search" placeholder="Suchbegriff eingeben" name="q" value="<?= html($query) ?>">
   <input type="submit" value="Suche">
</form>

<?php if ($results->isNotEmpty()): ?>

  <h3>Suchergebnisse für: <?= html($query) ?></h3>
  <?php foreach ($results as $result): ?>
    <a href="<?= $result->url() ?>">
      <?= $result->startbild()->toFile() ?>
      <?= $result->title() ?>
    </a>
  <?php endforeach ?>

<?php elseif($query): ?>

  Nothing was found.

<?php endif ?>

1 Like

Thank you very much @rasteiner.
But when i insert the else-function - the “Nothing was found.” will also be shown if the search has not yet been used.
Is there a way to display the text only after the search has been used and no matching results have been found?

Oh, that’s right.
I’ve changed the code above

@rasteiner PERFECT - thank you so much! :+1: