Deconst 0.1 documentation

Search

«  Assets   ::   Contents   ::   Advanced Topics  »

Search¶

All content that’s submitted to a Deconst instance is also indexed for search. In order to display search results in a Deconst site, you’ll need to implement a search results page within your control repository.

The search results page must be defined entirely by a Nunjucks template within your control repository. It can’t be submitted from any content repository. This is because, to actually perform the search and enumerate results, you need to use a custom Nunjucks filter that’s only available to control repository templates.

First, map a search path within your content map at config/content.json. It doesn’t need to map to an actual content ID. Instead, you’ll usually want to map it to null to use a fixed, empty metadata envelope.

{
  "books.horse": {
    "content": {
      "/": "https://github.com/user/library-welcome/",
      "/search/": null
    }
  }
}

Now route this path to the search template in config/routes.json.

{
  "books.horse": {
    "routes": {
      "^/": "default.html",
      "^/search/?": "search.html"
    }
  }
}

Finally, you’ll need to create the template that displays the results of a given search. Create it as you would any other template, but rather than render {{ deconst.content.envelope.body }}, invoke the search filter on the query parameter:

<h1>Your Search Results</h1>

{% set r = deconst.request.query.q|search %}

<h2>Your search had {{ r.total }} results in {{ r.pages }} pages.</h2>

{% for result in r.results %}
  <!-- Use attributes on the result object to generate a search result. -->
  <div>
    <a href="{{ result.url }}">{{ result.title }}</a>
    <p>{{ result.excerpt }}</p>
  </div>
{% else %}
  <!-- Remember to show something sensible if no results are found. -->
  <p>No results found.</p>
{% endfor %}

The search filter accepts optional keyword parameters:

  • pageNumber is the current page number, which defaults to 1.
  • perPage is the number of results to include in a single page, which defaults to 10.
  • categories is an array of strings that, if specified, constrain search results to envelopes with at least one matching category.
{% set query = deconst.request.query %}
{% set r = query.q|search(pageNumber=query.page, perPage=query.pageSize) %}

To submit searches from any page, create a form that populates the corresponding query parameters:

<form method="get" action="/search">
  <label for="q">Search: </label>
  <input id="q" name="q" type="text" value="{{ deconst.request.query.q }}">
</form>

«  Assets   ::   Contents   ::   Advanced Topics  »

© Copyright 2015, Ash Wilson, Kyle Kelley. Created using Sphinx 1.8.1.