TWiki Formatted Search Results

Inline search feature allows flexible formatting of search result

The %SEARCH{...}% variable documented in TWikiVariables has a fixed format for the search result, that is, a table consisting of topic names and topic summaries. Use the format="..." parameter to specify a customized format of the search result. The string of the format parameter is typically a bullet list or table row containing variables (such as %SEARCH{ "food" format="| $topic | $summary |" }%).

Syntax

Two parameters can be used to specify a customized search result:

1. header="..." parameter

Use the header parameter to specify the header of a search result. It should correspond to the format of the format parameter. This parameter is optional. Example: header="| *Topic:* | *Summary:* |"

2. format="..." parameter

Use the format parameter to specify the format of one search hit.
Example: format="| $topic | $summary |"

The field value of a form field; for example, $formfield(TopicClassification) would get expanded to PublicFAQ. This applies only to topics that have a TWikiForm

$percnt

Percent sign (%)

$rev

Number of last topic revision, e.g. 1.4

$n or $n()

New line

$web

Name of the web

$parent

Name of parent topic; empty if not set

$parent(20)

Name of parent topic, same hyphenation/shortening like $topic()

$createusername

Login name of topic revision 1.1, e.g. jsmith

$username

Login name of last topic update, e.g. jsmith

$locked

LOCKED flag (if any)

$nop or $nop()

Is a "no operation". This variable gets removed; useful for nested search

$text

Formatted topic text. In case of a multiple="on" search, it is the line found for each search hit.

$formfield(name, 30, ...)

Form field value, shortended to 30 characters with "..." indication

$formfield(name, 20, -<br />)

Form field value, hyphenated each 20 characters with separator "-<br />"

$formfield(name, 10)

Form field value, "- " hyphenated each 10 characters

$quot

Double quote ("). Alternatively write \" to escape it

$dollar

Dollar sign ($)

$pattern(reg-exp)

A regular expression pattern to extract some text from a topic (does not search meta data; use $formfield instead). In case of a multiple="on" search, the pattern is applied to the line found in each search hit.• Specify a RegularExpression that covers the whole text (topic or line), which typically starts with .*, and must end in .*• Put text you want to keep in parenthesis, like $pattern(.*?(from here.*?to here).*)• Example: $pattern(.*?\*.*?Email\:\s*([^\n\r]+).*) extracts the email address from a bullet of format * Email: ...• This example has non-greedy .*? patterns to scan for the first occurance of the Email bullet; use greedy .* patterns to scan for the last occurance • Limitation: Do not use .*) inside the pattern, e.g. $pattern(.*foo(.*)bar.*) does not work, but $pattern(.*foo(.*?)bar.*) does • Note: Make sure that the integrity of a web page is not compromised; for example, if you include an HTML table make sure to include everything including the table end tag

Everybody can edit any page, this is scary. Doesn"t that lead to chaos? Answer...

Nested Search

Search can be nested. For example, search for some topics, then form a new search for each topic found in the first search. The idea is to build the nested search string using a formatted search in the first search.

Here is an example. Let's search for all topics that contain the word "culture" (first search), and let's find out where each topic found is linked from (second search).

Now let's nest the two. We need to escape the second search, e.g. the first search will build a valid second search string. Note that we escape the second search so that it does not get evaluated prematurely by the first search:

Note: Nested search can be slow, especially if you nest more then 3 times. Nesting is limited to 16 levels. For each new nesting level you need to "escape the escapes", e.g. write $dollarpercntSEARCH{ for level three, $dollardollarpercntSEARCH{ for level four, etc.

Search with conditional output

A regular expression search is flexible, but there are limitations. For example, you cannot show all topics that are up to exactly one week old, or create a report that shows all records with invalid form fields or fields within a certain range, etc. You need some additional logic to format output based on a condition:

Specify a search which returns more hits then you need

For each search hit apply a spreadsheet formula to determine if the hit is needed