PontusHorn / Pico-Search

Simple search feature for pages in your Pico CMS
MIT License
22 stars 4 forks source link

Plugin shouldn't filter the pages array #10

Closed Tontyna closed 4 years ago

Tontyna commented 4 years ago

First of all: Thanks for this plugin.

In onPagesLoaded the plugin filters the $pages array and removes all pages without search_rank. This interferes with my theme that requires the complete pages array for its own purpose (cf. the pico default theme).

Commented out the filtering lines https://github.com/PontusHorn/Pico-Search/blob/b539f8e3596e6048aac9fee534565b91a2f505c3/40-PicoSearch.php#L111-L113

Modified my search.twig to do the filtering:

    {% if search_terms %}
      <!-- show pages having a page_rank > 0 -->
      <div class="SearchResults">
          {% set found = [] %}
          {% for page in pages if page.search_rank > 0 %}
              {% set found = found|merge([page]) %}
          {% endfor %}
          {% if found %}
              <h2>Search results for {{ search_terms|e('html') }}</h2>
              {% for page in found %}
                  <div class="SearchResult">
                      <h3><a href="{{ page.url }}">{{ page.title }}</a></h3>
                      {% if page.description %}<p>{{ page.description }}</p>{% endif %}
                  </div>
              {% endfor %}
          {% else %}
              <p>No results found for {{ search_terms|e('html') }}.</p>
          {% endif %}
      </div>
    {% endif %}

Theme works as expected again.

PontusHorn commented 4 years ago

Hey!

You're absolutely right and I've been thinking about changing my plugins to work this way for some time now. This is a good reason to finally do it 🙂

PontusHorn commented 4 years ago

The plugin is updated now as of ba5fabdfdc7031c69a3bef68a1dec74e4ee173dc.

Note that the solution is somewhat different than your suggestion. Instead of filtering the array manually, you run the pages array through a filter: {% set search_results = pages|apply_search %}. You can then use the resulting array as you wish.

Tontyna commented 4 years ago

Of course, a Twig filter is/was the right way to go :heart:

maikotz commented 4 years ago

Awesome, Thanks for rewriting it. Just encountered the same problem!