Case: search returns no results

I’m sure that I am missing something very minor here. I have set up a search form that I put in my site/snippets/header.php snippet:

<form action="<?php echo page('search')->url() ?>">
      <input type="search" name="q"><input type="submit" value="Search">

Here is my rough prototype site/templates/search.php template

<?php snippet('header') ?>

<section id="search">
  <?php foreach($results as $result): ?>
    <a href="<?php echo $result->url() ?>">
      <?php echo $result->title()->html() ?>
      <?php echo $result->text()->excerpt(100) ?>
  <?php endforeach ?>


<?php snippet('footer') ?>

And finally my site/controllers/search.php controller:


return function($site, $pages, $page) {

  $query   = get('q');
  $results = $site->search($query, 'title|text');

  return array(
    'query'   => $query,
    'results' => $results, 


Everything is running smoothly, but I want to be able to insert some text that basically says: Your search did not return any results or something to that effect on the Search page. How is this done?

Thanks in advance!

Paul B.

You could either use the pagination object and then count the items:

$pagination = $results->pagination();
$itemCount = $pagination->countItems();

Or just check if there are any items in $results using count().

Then in your template, display the results only if there are elements:

if($itemCount == 0) {
   echo "no results"
else {
 //display results

One again @texnixe, your infinite Kirby knowledge has saved the day.

Many Thanks

Paul B.