Hi All:
For those of you who are missing search capability in Radiant, I created
a
simple “Search” behavior for pages. It lets you create a search page
that
receives keywords as parameters and lists pages that match those
keywords.
It also has live search capabilities. It is wrapped into a Rails plugin,
so
all you need to do is to dump the plugin it into the ‘vendor/plugins’
directory of your Radiant installation and restart the instance.
The easiest way to get it is via Subversion:
$ cd vendor/plugins
$ svn co http://nobits.org/svn/rails/plugins/search_behavior
or unpack the tarball (http://nobits.org/files/search_behavior.tar.gz)
into
the same directory.
This time there are no unit tests yet.
You can find a demo at: http://nobits.org/
Please let me know any comments or suggestions. Below follows the usage
description, copied directly from the source.
Cheers,
Oliver
Usage:
The "Search" behavior adds search capabilities to a Radiant site. A
page with this behavior responds to search queries that are passed
to
it through the request parameter “q”. Each space separated word in
the
query string is treated as a keyword. The search result includes
all published pages whose titles or parts match (substring matching)
all of the keywords specified in the query.
Using the "Search" behavior it is possible to provide standard
form-based search as well as "live" search using the prototype
library.
When the corresponding search page is called with its canonical URL,
it
is rendered similar to a normal page, i.e. with its entire layout.
When, however, “/live” is appended to the URL the response is
rendered
from the “live” page part defined for the page without any layout.
To process search results the behavior provides the following tags:
<r:search:results> [content] </r:search:results>
Renders [content] only if the result set is not empty.
<r:search:results:each> [content] </r:search:results:each>
Renders [content] for every page in the result set. Within this
tag
default tags to access page attributes (i.e. <r:url/>,
<r:link/>,
r:title/>, <r:author/>, etc.) are valid and defined for the
currently processed page from the result set.
<r:search:empty> [content] </r:search:empty>
Render [content] only if the result set is empty
(e.g. "No match").
<r:search:query/>
The original search query string.
Additionally the behavior provides a tag to generate a simple search
form, though its use is not required. Search forms can also be
defined
in normal HTML by the user:
<r:search:form [label="Search term:"] [live="element-to-update]
[prototype="true"]/>
Generates a simple search form that submits its content to the
search page for which it was created. Additional attributes are
optional:
label:
Defines an alternative label for the search field, the
default
is “Search:”.
live:
Enables the form observer for live search and specifies the
ID
of the HTML element that is updated with the results.
prototype:
If its value is "true" the Prototype JavaScript library
shipped
with Radiant is explicitly loaded, but only if the live
search
is enabled.
Example:
Assume a page with slug "search" as a child of the root page. The
"body" page part of this page may look like:
<r:search:form />
<hr />
<r:search:empty>
No match.
</r:search:empty>
<r:search:results>
Results:
<ul>
<r:search:results:each>
<li>
<r:link/> by <r:author/>
</li>
</r:search:results:each>
</ul>
</r:search:results>
To extend this example to allow a "live" search from the header
snippet that is included into every page, (Typo style live
search),
two additional page parts are created for the search page:
The "live" page part is only rendered when search results are
returned from a "live" search request:
<ul>
<r:search:results:each>
<li><r:link/></li>
</r:search:results:each>
</ul>
The "form" page part is included into the header snippet,
described
below:
<r:search:form live="search-results" prototype="true"
label="Live search:" />
<div id="search-results"></div>
Since, the header snippet itself is not always rendered in the
context of a "Search" behavior the <r:search:form> tag is not
always
available. This prevents us from adding the form directly into the
header snippet and requires the creation of the “form” page part
for
the search page. This page part can be easily included into the
header snippet with the following lines:
<r:find url="/search">
<r:content part="form"/>
</r:find>
This finds the search page and renders its "form" page part in the
context of the "Search" behavior.
TODO:
* escape query string for inclusion in response page
* behavior based filter for search results