Make search terms available to template

This commit is contained in:
pontus.horn
2019-06-14 23:44:04 +02:00
parent 1adbcec56f
commit 39ca6381dd
2 changed files with 28 additions and 20 deletions

View File

@@ -171,4 +171,8 @@ class PicoSearch extends AbstractPicoPlugin
public function isLowValueWord($word) { public function isLowValueWord($word) {
return in_array(mb_strtolower($searchTerm), $this->getPluginConfig('low_value_words')); return in_array(mb_strtolower($searchTerm), $this->getPluginConfig('low_value_words'));
} }
public function onPageRendering(&$twig, &$twigVariables, &$templateName) {
$twigVariables['search_terms'] = $this->search_terms;
}
} }

View File

@@ -12,8 +12,8 @@ The search plugin should be executed before the pagination plugin (execution ord
## Installation ## Installation
* **Copy the file `40-PicoSearch.php` to the `plugins` sub-folder of your Pico installation directory.** - **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.** - **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 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: that loops through the pages and displays them. Your `search.md` might look like this:
@@ -23,23 +23,27 @@ The search plugin should be executed before the pagination plugin (execution ord
Template: search 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 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): lists the pages matching the search (substitute `paged_pages` for `pages` if using Pico-Pagination):
```twig ```twig
<div class="SearchResults"> {% if search_terms %}
{% if pages %} <div class="SearchResults">
{% for page in pages %} {% if pages %}
<div class="SearchResult"> <h2>Search results for {{ search_terms|e('html') }}</h2>
<h2><a href="{{ page.url }}">{{ page.title }}</a></h2> {% for page in pages %}
{% if page.description %}<p>{{ page.description }}</p>{% endif %} <div class="SearchResult">
</div> <h3><a href="{{ page.url }}">{{ page.title }}</a></h3>
{% endfor %} {% if page.description %}<p>{{ page.description }}</p>{% endif %}
{% else %} </div>
<p>No results found.</p> {% endfor %}
{% endif %} {% else %}
</div> <p>No results found for {{ search_terms|e('html') }}.</p>
{% endif %}
</div>
{% 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: 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:
@@ -67,7 +71,7 @@ template file:
```html ```html
<form id="search_form" action="{{ "search"|link }}"> <form id="search_form" action="{{ "search"|link }}">
<label for="search_input">Search the site:</label> <label for="search_input">Search the site:</label>
<input type="search" id="search_input" name="q" /> <input type="search" id="search_input" name="q" value="{{ search_terms|e('html_attr') }}" />
<input type="submit" value="Search" /> <input type="submit" value="Search" />
</form> </form>
<script type="text/javascript"> <script type="text/javascript">