From 39ca6381ddc343afd9384b42305f69f1b5612f27 Mon Sep 17 00:00:00 2001 From: "pontus.horn" Date: Fri, 14 Jun 2019 23:44:04 +0200 Subject: [PATCH] Make search terms available to template --- 40-PicoSearch.php | 4 ++++ README.md | 44 ++++++++++++++++++++++++-------------------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/40-PicoSearch.php b/40-PicoSearch.php index 84d9d86..f1757d0 100644 --- a/40-PicoSearch.php +++ b/40-PicoSearch.php @@ -171,4 +171,8 @@ class PicoSearch extends AbstractPicoPlugin public function isLowValueWord($word) { return in_array(mb_strtolower($searchTerm), $this->getPluginConfig('low_value_words')); } + + public function onPageRendering(&$twig, &$twigVariables, &$templateName) { + $twigVariables['search_terms'] = $this->search_terms; + } } diff --git a/README.md b/README.md index d7a3eec..5071ffd 100644 --- a/README.md +++ b/README.md @@ -12,44 +12,48 @@ The search plugin should be executed before the pagination plugin (execution ord ## Installation -* **Copy the file `40-PicoSearch.php` to the `plugins` sub-folder of your Pico installation directory.** -* **Add a file named `search.md` to your content root or the sub-folder you want to make searchable.** +- **Copy the file `40-PicoSearch.php` to the `plugins` sub-folder of your Pico installation directory.** +- **Add a file named `search.md` to your content root or the sub-folder you want to make searchable.** This is your search results page. You can leave it empty of content, but set the `Template` meta tag to a template that loops through the pages and displays them. Your `search.md` might look like this: - + ``` /* Title: Search results Template: search */ ``` -* **Add a template file with the name defined in `search.md`.** + +- **Add a template file with the name defined in `search.md`.** Your template file (`search.twig` in the above example) should contain something like the following section, which lists the pages matching the search (substitute `paged_pages` for `pages` if using Pico-Pagination): - + ```twig -
- {% if pages %} - {% for page in pages %} -
-

{{ page.title }}

- {% if page.description %}

{{ page.description }}

{% endif %} -
- {% endfor %} - {% else %} -

No results found.

- {% endif %} -
+ {% if search_terms %} +
+ {% if pages %} +

Search results for {{ search_terms|e('html') }}

+ {% for page in pages %} +
+

{{ page.title }}

+ {% if page.description %}

{{ page.description }}

{% endif %} +
+ {% endfor %} + {% else %} +

No results found for {{ search_terms|e('html') }}.

+ {% endif %} +
+ {% endif %} ``` If you simply want to make your search results page look like your standard page, you may want to edit your theme's `index.twig` file and change `{{ content }}` to `{% block content %} {{ content }} {% endblock %}`. This allows you to extend this base template and reuse all the other parts of it in your search results template: ```twig {% extends "index.twig" %} - + {% block content %} {{ parent() }} - +
@@ -67,7 +71,7 @@ template file: ```html
- +