Template operators

mnoGoSearch template language provides a set of operators making it possible to create templates with some logic.

Conditional operators

mnoGoSearch supports a number of conditional operators in search templates: IF, IFCS, IFNOT, IFLIKE, ELSEIF (ELIF), ELSELIKE (ELIKE), IFLE, IFLT, IFGE, IFGT.

Comparison is performed case insensitively for the operators IF, IFNOT, IFLIKE, ELSEIF, ELSELIKE, and case sensitively for IFCS. The operators IFLE, IFLT, IFGE, IFGT perform numeric comparison: less-or-equal, less, greater-or-equal and greater comparison correspondingly.

Examples:


<!IF   NAME="Content-Type" Content="application/pdf">
<img src="pdf.png">
<!ELIF NAME="Content-Type" Content="text/plain">
<img src="text.png">
<!ENDIF>

<!IFLIKE NAME="URL" CONTENT="http*">
This is an HTTP address
<!ELIKE  NAME="URL" CONTENT="ftp*">
This is an FTP address
<!ELSE>
This is an unknown address type>
<!ENDIF>

It is possible to use nested conditional operators. This gives much power for search template construction. You can find some examples in the default template search.htm-dist.

The SET operator

This operator is designed to set a variable value from a constant, from another variable, or from a more complex expression.

Examples:


<!SET NAME="a" Content="Some string">
<!SET NAME="b" Content="Another string">
<!SET NAME="c" Content="$(a)">
<!SET NAME="d" Content="a is '$(a)', b is '$(b)', c is '$(c)'">
      

The COPY operator

The COPY operator calculates the expression given in the attribute CONTENT, then treats this value as a name of another variable, whose value is then copied into to the variable with the name given in the attribute NAME.

Examples:


<!SET NAME="v1" CONTENT="The first value">
<!SET NAME="v2" CONTENT="The second value">
<!SET NAME="v3" CONTENT="The third value">
<!SET NAME="i"  CONTENT="2">
<!COPY NAME="value" CONTENT="v$(i)">
Value="$(value)"
The above code will display this text:

Value="The second value"

Arithmetic operators

Arithmetic operators INC and DEC respectively increment and decrement a variable value treating it as an integer number.

Examples:


<!SET NAME="a" Content="10">a is $(a)
<!INC NAME="a">After increment, a is $(a)
<!DEC NAME="a">After decrement, a is $(a)

Arithmetic operators ADD, SUB, MUL perform integer addition, subtraction and multiplication of two variables specified in the NAME and CONTENT attributes and write the result back into the variable specified in the NAME attribute.

Examples:


<!SET NAME="b" CONTENT="20">
<!SET NAME="a" CONTENT="10">
<!MUL NAME="a" CONTENT="$(b)">a*b=$(a)
<!SET NAME="a" CONTENT="10">
<!ADD NAME="a" CONTENT="$(b)">a+b=$(a)
<!SET NAME="a" CONTENT="10">
<!SUB NAME="a" CONTENT="$(b)">a-b=$(a)             

Loop operators

Three loop operators WHILE, WHILENOT and FOR are available. The FOR operator was introduced in mnoGoSearch 3.3.0.

Examples:


<!SET      NAME="a" Content="10">
<!WHILENOT NAME="a" Content="0">
  a is $(a)
<!DEC NAME="a">
<!ENDWHILE>

<!FOR NAME="a" FROM="10" TO="20">a=$(a)<!ENDFOR>

<!SET NAME="from" CONTENT="80">
<!SET NAME="to" CONTENT="90">
<!FOR NAME="a" FROM="$(from)" TO="$(to)">a=$(a)<!ENDFOR>

Miscellaneous operators and functions

URLDECODE - decodes an URL-encoded string.

All instances of the %## encoding in the given string are decoded and the decoded string is written into the variable specified in the NAME attribute.

Examples:


<!URLDECODE NAME="decoded" Content="$(url)">URL is $(decoded)
      

HTMLENCODE - converts special characters to HTML entities

. The conversion rules are:

Examples:


<!HTMLENCODE NAME="encoded" Content="$(url)">URL is $(encoded)
      

EREG - Replaces a regular expression.


<!EREG NAME="a" CONTENT="string" MATCH="pattern" RESULT="replacement">

EREG scans the string given in the CONTENT attribute for matches to the string given in MATCH and stores the matched text into variable given in NAME using the RESULT value as a replacement pattern. The replacement string can contain substrings in the form $N, where N is a decimal digit 0..9, which is replaced to the text matching the N-th parenthesized substring. $0 produces the entire contents of matching string. The CONTENT string can have references to other variables.

Examples:


<!SET NAME="str" CONTENT="http://www.host.com/path/file.ext">
<!EREG NAME="a" CONTENT="$(str)"
MATCH="^([a-z]*)://([^/]*)/(.*)" RESULT="scheme=$1; host=$2
path+file=$3">$(a)
will display:

scheme=http host=www.host.com path=path/file.ext

EREGCUT - Removes a regular expression.


<!EREGCUT NAME="a" CONTENT="string" MATCH="pattern">

EREGCUT scans the string given in the CONTENT attribute for matches to the pattern given in MATCH and stores the value of CONTENT with all matched parts removed into the variable given in NAME.

The CONTENT string can have references to other variables.

If CONTENT does not match the pattern given in MATCH, the entire value of CONTENT is copied to NAME:


<!-- This does not match and preserves the original value. -->
<!EREGCUT NAME="a"
  CONTENT="http://www.host.com/path/file.ext"
  MATCH="[?].*">$(a)
displays:

http://www.host.com/path/file.ext

If CONTENT matches, and the pattern in MATCH has parenthesized sub-expressions, then only the pieces matching to the sub-expressions are removed:


<-- Remove the "a" and the "e" parameters from the query string -->
<!EREGCUT NAME="a"
  CONTENT="http://www.host.com/path/file.ext?a=b&c=d&e=f&g=h"
  MATCH="[?&](a=[^&]*&).*&(e=[^&]*&)">$(a)
displays:

http://www.host.com/path/file.ext?c=d&g=h

If CONTENT matches, and the pattern does not have parenthesized sub-expressions, then the entire matching part is removed:


<-- Remove the query part from the URL -->
<!EREGCUT NAME="a"
  CONTENT="http://www.host.com/path/file.ext?a=b&c=d&e=f&g=h"
  MATCH="[?].*">$(a)
displays:

http://www.host.com/path/file.ext