Skip to content

Instantly share code, notes, and snippets.

@ohnonot
Created March 16, 2019 06:45
Show Gist options
  • Save ohnonot/19290cf1380c5d4d368f23b56c84bd46 to your computer and use it in GitHub Desktop.
Save ohnonot/19290cf1380c5d4d368f23b56c84bd46 to your computer and use it in GitHub Desktop.
Simple search for picocms with inbuilt pagination
{% extends "index.twig" %}
{% set sstring = url_param('q','string' ) %}
{% block content %}
<div class="search_results" id="res">
<p class="small">The search function is primitive. It only searches for the literal string as entered.<br>
» fluffy cloud<br>
will search only the exact string<br>
» "fluffy cloud".<br>
Searches are case insensitive. Tags and some special characters are stripped.</p>
{% if sstring|length < config.tagblog.ssminlength %}
<h4>The search string "{{sstring}}" is too short.</h4><p>At least {{config.tagblog.ssminlength}} searchable characters, please try again.</p>
{% else %}
{% set sstring = sstring|lower %}
{% set results = {} %}
{% for page in pages if page.meta.tags %}
{% if sstring in page.id|content|striptags|lower or sstring in page.title|striptags|lower or sstring in page.id|lower %}
{% set results = results|merge([page]) %}
{% endif %}
{% endfor %}
{% set count = results|length %}
<h4>{{ count }} search results for "{{sstring}}"</h4>
{% if config.tagblog.paging > 0 and count > config.tagblog.paging %}
{% set p = url_param('p','int',1) %}
{% set offset = (p - 1) * config.tagblog.paging %}
{% for page in results|slice(offset,config.tagblog.paging) %}
{% include 'postlistitem.twig' %}
{% endfor %}
<div class="pagination">
{% if offset >= config.tagblog.paging %}
<a href="/search?q={{sstring}}&p={{ p - 1 }}#res">Previous Page</a>
{% endif %}
{% if offset >= config.tagblog.paging and offset + config.tagblog.paging < count %} | {% endif %}
{% if offset + config.tagblog.paging < count %}
<a href="/search?q={{sstring}}&p={{ p + 1 }}#res">Next Page</a>
{% endif %}
</div>
{% else %}
{% for page in results %}
{% include 'postlistitem.twig' %}
{% endfor %}
{% endif %}
{% endif %}
</div>
{% endblock content %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment