docs.roxen.comView this page in a printer friendly mode
DocsRoxenWebServer 4.5TutorialsIf tags
Copyright  2012, Roxen Internet Software
Suggestions, comments & compliments
manuals@roxen.com
 DEMO  DOCS  PIKE
 COMMUNITY  DOWNLOAD
www.roxen.com



The basics of if-else
If tags syntax
If plugins
A basic example
<if> and <define>
<if supports>
Summary

The syntax of If tags

This section will introduce the basics of the RXML If tags.

After reading this section you will know the proper usage of RXML If tags using attributes and operators.

If you are looking for an example, see A basic example of <if>.

Tags

If-statements in RXML are built up by combining the six basic tags <if>, <else>, <elseif>, <then>, <true> and <false>. With <if> and <elseif>, attributes are used to state which test(s) to do. One attribute should be one of several If plugins. Logical attributes and, or and not are used to further specify the test(s). We sum this up in the following general syntax (code within '[ ]' is optional):


<if plugin1='expr' [and|or plugin2='expr' ...] [not]>
  if block
</if>
[<elseif plugin='expr' ...>
  elseif block
</elseif>]
[<else>
  else block
</else>]

or

<if plugin1='expr' [and|or plugin2='expr' ...] [not] />
<then>
  if block
</then>
[...]

Always remember to close tags


<if variable='var.foo = 10' />
  or
<if variable='var.foo = 10'></if>

and to add values to attributes


<if true=''></if>
----------------------
<if variable='var.foo = 1' and='' match='&var.bar; is bar'>

for proper XML syntax. This is necessary since RXML is a XML compliant language.

Let's have a look at the basic tags:

<if>

is used to conditionally show its contents; provides a framework for the If plugins.

<else>

shows its contents if the previous <if> returned false, or if there was a <false> above.

<elseif>

is the same as <if>, but will only evaluate if the previous <if> returned false.

<then>

shows its contents if the previous <if> returned true, or if there was a <true> above.

<true>

is an internal tag used to set the return value of If tags to true.

<false>

is an internal tag used to set the return value of If tags to false.


<set variable='var.foo' value='1' />

<if variable='var.foo = 1'>
  var.foo is 1
</if>
<else>
  var.foo is not 1
</else>
var.foo is 1

A test is made if the variable var.foo is 1. This is true because the first line does nothing less than sets that variable to 1. Please note that the spaces around the '=' operator are mandatory.


<set variable='var.foo' value='1' />

<if variable='var.foo = 1' />
<then>
  var.foo is 1
</then>
<else>
  var.foo is not 1
</else>
var.foo is 1

The same test using if-then-else instead.


<true />
<then>
  truth value is true
</then>
<else>
  truth value is false
</else>
truth value is true

In this example the internal <true> is used to set the truth value to true so that the following <then> will be executed.

Attributes

The attributes used with <if> are:

plugin name

The If plugin that should be used to test something, e.g. <if variable>. It is mandatory to specify a plugin. See the If plugins Section for further information.

not

Inverts the result (true->false, false->true).

or

If any criterion is met the result is true.

and

If all criterions are met the result is true. and is default.


<set variable='var.foo' value='1' />

<if variable='var.foo = 1' not=''>
   var.foo is not 1
</if>
<else>
   var.foo is 1
</else>
var.foo is 1

Here the test is logically negated with not.

The use of <if variable='var.foo != 1'> gives the the same result as <if variable='var.foo = 1' not=''>.

A common mistake done is when combining and, or and not.


<if user='foo' or='' not='' domain='*.foobar.com'>
  ...
</if>

will not work since the not attribute negates the whole tag, not just the domain attribute. If you want to negate the whole condition, add not at the end. If you only want to negate one of the attributes, you must rewrite the code with an if-elseif-else statement.


<if user='foo'>
</if>
<elseif domain='*.foobar.com' not=''>
  ...
</elseif>

<set variable='var.length' value='3' />
<set variable='var.string' value='foo' />

<if variable='var.length > 0' <b>and=''</b> match='&var.string; = foo'>
   var.string is 'foo'
</if>
<else>
   Either string is empty, doesn't contain string 'foo' or both.
</else>
var.string is "foo"

A multiple test with two different If plugins, variable and match.

You could be tempted to write expressions like:


<if variable='var.foo = 1' or='' variable='var.bar = 1'>
  ...
</if>

This will not work, as you can only use the same attribute once. Here Variable is used twice.

Operators

Above we used the '>' operator. The operators that may be used in expressions are:

OperatorMeaning

=

equals

==

equals

is

equals

!=

not equals

<

less than

>

greater than

Note that '<=' and '>=' are not possible operators except in the Expr plugin. However, for the effect of <if variable='var.foo <= 1'> you simply use <if variable='var.foo > 1' not=''> instead.

Global patterns are possible to use in almost all plugin expressions. ' * ' match zero or more arbitrary characters while ' ? ' matches exactly one arbitrary character.


<if ip='130.236.*'>
  Your domain is liu.se
</if>
<else>
  Your domain isn't liu.se
</else>
Your domain isn't liu

In this example 130.236.1 as well as 130.236.123 would be true. If the test would be <if ip='130.236.?'> only 130.236.0 - 9 would be true.

Regular expressions are not supported in If tags.

Summary

This section has taught you the basics of the If tags. If statements are built up by the six basic tags, <if>, <else>, <elseif>, <then>, <true> and <false>.

With <if> and <elseif> an attribute naming an If plugin must be added. The general syntax is:


<if plugin1='expr' [and|or plugin2='expr' ...] [not]>
  if block
</if>
[<elseif plugin='expr' ...>
  elseif block
</elseif>]
[<else>
  else block
</else>]

Logical attributes, and, or and not adds functionality. Inside the attribute expression, the '=', '==', 'is', '!=', '<' and '>' operators are valid.

Always remember to close tags ( / ) and to give attributes a value ( ='' ) for proper XML syntax.

More details about If tags is found in the Web Site Creator Manual or by putting <help for='if' /> in a web page.

The next section, If plugins will explain the usage of the If plugins.