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

If plugins

This section will introduce the If plugins used as attributes with <if> and <elseif>.

After reading this section you will have knowledge of the different plugin categories and the usage of the plugins.

The following sections will contain examples of how to use different plugins for creating dynamic web pages in many different ways.

The categories

The If plugins are sorted into four categories according to their function: Eval, Match, State and Utils.

EVAL MATCH STATE UTILS

clientvar

accept

config

date

cookie

client

false

exists

expr

domain

pragma

group

match

ip

prestate

time

variable

language

supports

user

.

referrer

true

.

.

.

.

.

The following parts will go through these categories and the corresponding plugins. We will look at the proper way of usage and some traps you might fall into.

Eval

The Eval category is the one corresponding to the regular tests made in programming languages, and perhaps the most used. They evaluate expressions containing variables, enities, strings etc and are a sort of multi-use plugins. All If-tag operators and global patterns are allowed (see The syntax of If tags).

PluginSyntax

clientvar

clientvar='var[ is value]'

cookie

cookie='name[ is value]'

expr

expr='pattern'

match

match='pattern1[,pattern2,...]'

variable

variable='name[ is pattern]'

Cookie and Variable plugins use a similar syntax. is can be any valid operator and name is the name of any defined or undefined variable(cookie). They first check if the named variable(cookie) exists, and if it does and a pattern(value) is specified, the expression will be evaluated. Variable is a general plugin while Cookie is for testing existence and/or values of cookies.


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

<if variable='var.foo = 10'>
  true
</if>
<else>
  false
</else>
true

Please note that it is the name of the variable, not the entity representing it, that should be used. Here we receive an error from the RXML parser becase we used an entity instead of a variable name. The proper way to do the test above is <if variable='var.foo = 10'>.

Match is used in a similar way, but there are certain differences. The syntax is <if match="pattern">, where pattern could be any expression. We could use <if match='var.foo = 10'> although this is rather meaningless, since we would check if the name of the variable var.foo (or the string "var.foo") was equal to the string "10", which it obviously is not. The only thing that would return true in this case is another meaningless expression: <if match="var.foo is var.foo">.

Instead we should always use entities with Match, i.e. &var.foo;. In RXML, entities are used to represent the contents of something, e.g. a variable. An entity is written as a variable name enclosed with '&' and ';'. Above the name of the variable is var.foo, the value is '10' and the entity is &var.foo;. The entity is replaced by its content when parsed to HTML, in this case '10'.


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

<if match='var.foo = 10'>
  true
</if>
<else>
  false
</else>
true

Again, variable name goes with Variable and entity goes with Match.

A warning when testing patterns with whitespaces. If you want to test if the entity &var.foo; equals the string 'he is nice' and do like this...


<set variable='var.foo' value='he is nice' />
'&var.foo;' <br /><br />

<if match='&var.foo; is he is nice'>
  var.foo is 'he is nice'
</if>
<else>
  var.foo is not 'he is nice'
</else>
"he is nice" var.foo is not "he is nice"

...you won't get the expected result. This is because the Match plugin interprets the first valid operator after a whitespace as the operator to use. In the example above the test really is if 'he' equals 'nice is he is nice', which obviously is false. Rember that the string 'is' also is a valid operator.

Expr evaluates mathematical and logical expressions. The following characters only should be used in the expression:

1, 2, 3, 4, 5, 6, 7, 8, 9, 0, ., x, a, b, c, d, e, f, X, A, B, C, D, E, F

For numbers, decimal, octal and hexadecimal. E.g. 1.23, 010 == 8, 0xA1 == 161

int, float

For type casting between integers and floats. E.g. (int)3.45 == 3, (float)3 == 3.000000

<, >, =, -, +, *, /, %, ( ,)

Mathematical operators. Note that '==' should be used for equality. E.g. 10 % 4 == 2 , (1 + 2) * 3 == 9

&, |

Logical operators. Note that '&&' and '||' should be used for 'and' and 'or', that '1' equals 'true' and '0' equals 'false'. E.g. 1+0 && 1

If any other character is used there will be an error in the RXML parser. Therefore entities should be used, not the variable name, in a similar way as Match.


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

<if expr='1+&var.foo;*3==7'>
   var.foo = 7
</if>
<else>
   var.foo = 9
</else>
var.foo = 7

An example of expr showing that the regular rules of mathematics applies here. The expression evaluates correctly as

1+2*3 == 1+6 == 7

and not as

1+2*3 == 3*3 == 9

Note that the '==' operator must be used for 'equals', unlike the other Eval plugins. Also note that whitespaces around operators are not mandatory.

Clientvar extends the Supports plugin, see State below. It is used for tests of the client requesting the web page, e.g. a browser or a WAP phone. The following variables (var) are currently testable:

  • height - The presentation area height in pixels (WAP clients)

  • javascript - The highest version of javascript supported

  • robot - The name of the web robot

  • width - The presentation area width in pixels (WAP clients)

is can be any valid operator.


<if supports='javascript'>
  <if clientvar='javascript < 1.2'>
    Your browser supports older versions of javascript
  </if>
  <else>
    Your browser supports javascript
  </else>
</if>
<else>
  Your browser doesn't support javascript at all
</else>

The variable can be used in expressions as shown above. Clientvar can test the exact JavaScript version supported, unlike Supports, that only checks if JavaScript is supported or not.

Match

The Match category contains plugins that match contents of something, e.g. an IP package header, with arguments given to the plugin as a string or a list of strings.

PluginSyntax

accept

accept='type1[,type2,...]'

client

client='pattern'

domain

domain='pattern1[,pattern2,...]'

ip

ip='pattern1[,pattern2,...]'

language

language='language1[,language2,...]'

referrer

referrer='pattern1[,pattern2,...]'

Accept checks which content types the browser accepts as specified by it's Accept-header, e.g. 'image/jpeg' or 'text/html'.


<p>You are using
<if client='Mozilla*'>
  <if client='*compatible*msie*'>
    <b>Internet Explorer</b>.
  </if>
  <elseif client='*compatible*opera*'>
    <b>Opera</b>.
  </elseif>
  <else>
    <b>Netscape</b>.
  </else>
</if>
<else>
  another client.
</else>
</p>

Client compares the user agent string with the given pattern.

Domain and Ip plugins checks if the DNS name or IP address of the user's computer match any of the patterns specified. Note that domain names are resolved asynchronously, and that the first time the browser accesses a page containing <if domain>, the domain name might not have been resolved yet.

Language matches languages with the Accept-Language header.

Referrer checks if the Referrer header match any of the patterns.

State

State plugins check which of the possible conditions something is in, e.g. if a flag is set or not, if something is supported or not, if something is defined or not etc.

PluginSyntax

config

config='name'

false

false=''

pragma

pragma='string'

prestate

prestate='option1[,option2,...]'

supports

supports='feature'

true

true=''

True and False are plugins used in exactly the same way as <then> and <else>. Should not be confused with the <true> and <false> tags.


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

<if variable='var.foo is 10' />
<if true=''>
  var.foo is 10
</if>
<if false=''>
  var.foo is not 10
</if>
var.foo is 10

Config tests if the RXML config named has been set by use of the <aconf> tag.


<if pragma='no-cache'>
  The page has been reloaded!
</if>
<else>
  Reload this page!
</else>

Pragma compares the HTTP header pragma with the given string.

Prestate checks if all of the specified prestate options, e.g. '(debug)', are present in the URL.

Supports tests if the client browser supports certain features such as frames, cookies, javascript, ssl among others. An example of this with a complete list of features that can be tested is found in the section Browser independency with <if supports>

Utils

The Utils category contains additional plugins, each specialized in a certain type of test.

PluginSyntax

date

date='yyyymmdd' [before,after] [inclusive]

exists

exists='path'

group

group='name' groupfile='path'

time

time='hhmm'' [before,after] [inclusive]

user

user='name1[,name2,...] [any]'


<if time='1200' before='' inclusive=''>
   ante meridiem (am)
</if>
<else>
   post meridiem (pm)
</else>

Date and Time plugins are quite similar. They check if today is the date "yyyymmdd" or if the time is "hhmm". The attributes before, after and inclusive may be added for wider ranges.


<if exists='/dir1/dir2/foo.html'>
   foo.html exists
</if>
<else>
   foo.html doesn't exist
</else>
foo.html doesn't exist

Exists checks if a file path exists in a file system. If the entered path does not begin with '/', it is assumed to be a URL relative to the directory containing the page with the <if exists> statement.

Group checks if the current user is a member of the group according the groupfile.

A useful Util plugin is User, that tests if the user accessing a site has been authenticated as one of the users specified. The argument any can be given for accepting any authenticated user.

Summary

This Section has presented the If plugins that is divided into four categories, Eval, Match, State and Utils, Utils and SiteBuilder, according to their function.

Eval plugins evaluate expressions as in regular programming languages, Match plugins match contents with arguments given and State plugins check which of two possible conditions is met. Utils plugins perform specific tests such as present date or time.

More details about If plugins are found in the Web Site Creator Manual.

The next Section, A basic example of <if>, will show an example of a basic usage of the Match plugin.