Search function on kirby

Sorry for being dumb but I really didnt get it. How to implement the search function on my site?
I followed the documentation https://getkirby.com/docs/cookbook/search but theres is no description how to set the search field on the site? I even cant say “content site search just use the right template”. Please I need a description for this.

I’m afraid I don’t quite get what you want to change? If you want the search form to appear on every page, put the form in a snippet you include in the header or footer and set the action attribute to the search page.

No thats is not the solution. If I place this form on my site it still not works. The variable dont appear in there?

<?php echo esc($query) ?>

Two options:

Remove the value part, probably doesn’t make sense if the form is not on the same page as the results.

Otherwise, you have to either include the logic in the snippet or pass the query variable to the new results page.

hm. I did all the things that where post on / docs / cookbook / search
except the modifikation of the result.

But nothing happened by pushing the search button. It´s not realy important to get the variable in the searchfield.
I add this controler thing, I add this template thing and create the content site…

What else should I do? I really did all the things on / docs / cookbook / search
Is there anything more to do?

Ok, let’s start at the beginning.

If you really did everything like explained in the docs, your search form should work. Maybe you can post your code here including the filenames so we can try to find the problem.

I copy paste it, like its explained in the docs. Nothing more, nothing less.
I cant choose search template for the search-content-site because there is no .yml file. But this wasnt explained in the docs.

I really dont add any other code to the given code in the docs.

Template:

<?php snippet('header') ?>

<form>
  <input type="search" name="q" value="<?php echo esc($query) ?>">
  <input type="submit" value="Search">
</form>

<ul>
  <?php foreach($results as $result): ?>
  <li>
    <a href="<?php echo $result->url() ?>">
      <?php echo $result->title()->html() ?>
    </a>
  </li>
  <?php endforeach ?>
</ul>

<?php snippet('footer') ?>

Controllers:

<?php

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

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

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

};

Content:

<form>
  <input type="search" name="q" value="<?php echo esc($query) ?>">
  <input type="submit" value="Search">
</form>

<ul>
  <?php foreach($results as $result): ?>
  <li>
    <a href="<?php echo $result->url() ?>">
      <?php echo $result->title()->html() ?>
    </a>
  </li>
  <?php endforeach ?>
</ul>

sorry - the forum software did strange thing with my posted code :-/

What is the content file and why does it contain the same stuff as the template? Is that the content search.txt file? Then you can’t put php code into it.

The content file in the /content/search folder should be called search.txt and only contain the page title.

This was for testing it, because of the form.
Now I set the form in the footer. variable works -> the searchfield is plain. But it still not works.

Ah okay, so I can open the content search site. I have to clear the content/search/search.txt and add the title.

But I want to place the form into my header.

It feels that there is no connection to the searching script.
URL should be /search?q=test but it is /?q=test

As I said above, then your search form has to have an action attribute:

<form class="search" action="<?= page('search')->url() ?>">
1 Like

Yeah! Thank you very much :slight_smile:

I guess this all should be explained in the docs. Its not clear to add all these things to the search form.
Anyway thank you for your help.