@search
Searches for nodes and makes those nodes available to the template.
Parameters
parameter | required | description |
---|---|---|
text | required | the text to search for |
sort | the field to sort on | |
sortDirection | the direction to sort in (either 1 for ascending or -1 for descending) | |
limit | the number of records to return | |
skip | a position to skip ahead to in the record set | |
scope | if "page", then only relatives related to the current page will be returned | |
as | specifies the name of the results variable (by default - rows ) |
|
locale | the locale to use in retrieving content |
Response
The result is an associative array of key/value pairs for each of your nodes. Each key is the resulting node ID and each value
is the JSON for the node.
Examples
Example #1: Restaurant Names
Here is an example where we retrieve nodes of type custom:restaurant
. We use this to build out a table.
Each row of the table shows the restaurant's node ID and title.
{@search sort="title" skip="0" limit="5" text="Shakey's Pizza"}
<table>
{#rows}
<tr>
<td>{_doc}</td>
<td>{title}</td>
</tr>
{/rows}
</table>
{/search}
Example #2: Restaurant Names with Thumbnail Images
Here is an example that lists restaurants and shows an thumbnail image for each:
{@search sort="title" skip="0" limit="5" text="Shakey's Pizza"}
<table>
{#rows}
<tr>
<td>
{#attachments.default}
<img src="{preview64}">
{/#attachments.default}
</td>
<td>{title}</td>
</tr>
{/rows}
</table>
{/search}
Example #3: Using pagination
Here is an example that searches for the text 'August' and hands back the first 10 results.
The results are passed to a reusable template called 'template4'.
{<template4}
<table>
{#rows}
<tr>
<td>{_doc}</td>
<td>{title}</td>
</tr>
{/rows}
</table>
{/template2}
{@search sort="title" skip="0" limit="10" text="August"}
{+template4/}
{/search}
And here is an example that does the same thing but utilizes the @params tag to pass an additional variable
(category
) to the template.
The template uses the special {$idx}
dust variable to jot down the row count as the rows are iterated through.
{<template5}
<table>
{#rows}
<tr>
<td>{$idx}</td>
<td>{category}</td>
<td>{_doc}</td>
<td>{title}</td>
</tr>
{/rows}
</table>
{/template2}
{@search sort="title" skip="0" limit="10" text="August"}
{@params category="month"}
{+template5/}
{/params}
{/search}
The two examples above both use a referenced template. You can also inline templates. For cases where you don't need to reuse templates, this makes sense. It reduces the amount of code. You can do it like this:
{@search sort="title" skip="0" limit="10" text="August"}
{@params category="month"}
<table>
{#rows}
<tr>
<td>{$idx}</td>
<td>{category}</td>
<td>{_doc}</td>
<td>{title}</td>
</tr>
{/rows}
</table>
{/params}
{/search}