Hi,
i followed the search tutorial on the kirby site and get a simple search running.
But how can i show a nothing found message? I tried with if results empty but that didn’t work.
<ul>
<?php foreach($results as $result): ?>
<li>
<a href="<?= $result->url() ?>">
<?= $result->title()->html() ?>
</a>
</li>
<?php endforeach ?>
</ul>
thanks
Apparently there is no $results->empty()
or $results->isEmpty()
!?
So my guess is, that you have to check for the count of elements in the collection of pages (which should be the type of the $results
variable).
if ($results->count() == 0) {
// show "nothing found"
} else {
// show results (foreach ...)
}
https://getkirby.com/docs/cheatsheet/pages/count
These https://getkirby.com/docs/toolkit/api#collection should be all the methods of the
2 Likes
On one of my sites I used the pagination to check if there were results.
Like this:
if ($query && !$pagination->pages()) :
snippet('message');
else:
snippet('grid' , [ $posts , $pagination ]);
endif;
In my case I had to check for the query variable because the template is shared with other pages.
If you have a dedicated template for the search results you can simply check for the number of pages.
1 Like
Hi and thanks for your answers!
i did was jbeyerstedt said and count it. This seems to work with one little problem. When the site loads the text “nothing found” is already there. I tried to use “if isset” but this gave men an error that i can’t do that.
<ul class="list-group">
<?php if ($results->count() == 0): ?>
<p>Nothing found</p>
<?php else: ?>
<?php foreach($results as $result): ?>
<li class="list-group-item">
<a href="<?= $result->url() ?>">
<?= $result->title()->html() ?>
</a>
</li>
<?php endforeach ?>
<?php endif ?>
</ul>
At first, I think a <p>
tag inside <ul>
is not valid html. But this shouldn’t be the real issue.
Do I understand you correctly, that the “Nothing found” text is there at first and then gets replaced by the search results?
This seems quite odd and my only guess is some kind of caching.
So, could you please disable all browser caching and also disable caching for this page in your kirby config.php
. (see cache.ignore
The browser caching can be disabled by opening the developer utilities in your favorite browser, getting to the network page and there should be a tick-box to disable the cache.
Could you provide your complete code, please? Maybe $results is. empty when you first load the page and. you only get results once your fill in the form? I.e. your first load with nothing found is probably not. a cache issue but happens if your call the page. without submitting the search form?
BTW. apart from the issue with the p tag inside a list element, I’d wrap the if statement around the list, so you don’t get an empty list in case there are no results:
<?php if ($results->count()): ?>
<ul class="list-group">
<?php foreach($results as $result): ?>
<li class="list-group-item">
<a href="<?= $result->url() ?>">
<?= $result->title()->html() ?>
</a>
</li>
<?php endforeach ?>
</ul>
<?php else: ?>
<p>Nothing found</p>
<?php endif ?>
And depending on whether this search template is only called after a search or in any case, maybe use an additional outer if statement that checks if a query was submitted.
<?php if($query): ?>
<?php if ($results->count()): ?>
<ul class="list-group">
<?php foreach($results as $result): ?>
<li class="list-group-item">
<a href="<?= $result->url() ?>">
<?= $result->title()->html() ?>
</a>
</li>
<?php endforeach ?>
</ul>
<?php else: ?>
<p>Nothing found</p>
<?php endif ?>
<?php endif ?>
1 Like