Files
liquid/_posts/objects/search.md
2014-07-23 11:17:11 -04:00

2.3 KiB

layout, title, nav
layout title nav
default search
group
Liquid Variables

search

The search object has the following attributes:

{% table_of_contents %}

{% anchor_link "search.performed", "search-performed" %}

Returns true if an HTML form with the attribute action="/search" was submitted successfully. This allows you to show content based on whether a search was performed or not.

{% highlight html %}{% raw %} {% if search.performed %} {% endif %} {% endraw %}{% endhighlight %}

{% anchor_link "search.results", "search-results" %}

Returns an array of matching search result items. The items in the array can be a:

You can access the attributes of the above three objects through search.results.

{% highlight html %}{% raw %} {% for item in search.results %}

{{ item.title | link_to: item.url }}

{% if item.featured_image %} {% endif %} {{ item.content | strip_html | truncatewords: 40 | highlight: search.terms }} {% endfor %} {% endraw %}{% endhighlight %}

{% anchor_link "search.results_count", "search-results_count" %}

Returns the number of results found.

{% anchor_link "search.terms", "search-terms" %}

Returns the string that was entered in the search input box. Use the highlight filter to apply a different style to any instances in the search results that match up with search.terms.

Input

{% highlight html %}{% raw %} {{ item.content | highlight: search.terms }} {% endraw %}{% endhighlight %}

Output

{% highlight html %}{% raw %} Yellow shirts are the best! {% endraw %}{% endhighlight %}