If Context

[If Expressions][Then]do this[/Then][Else]otherwise this[/Else][/If]

New in 4.0
Displays HTML or executes WebDNA conditionally only if the expression is true.

To display some HTML (or execute WebDNA [xxx] tags) only if certain conditions are met, place the text inside an [If] context. The comparison, which may contain any [xxx] tags, is first evaluated to see if it is true, and if true then the contained [Then] context is executed (or simply displayed, if it's just HTML). If not true, then the contained [Else] context is executed (or simply displayed, if it's just HTML). See [Then] and [Else].

Example (normally you would put the following text into a .tpl file on your server and use a web browser to link to it):

[If (("[username]"="Grant") | ([grandTotal]<100)) & ({[date]}<{2/15/2000})]
    [Then]either username was Grant or grandTotal was < $100 and it's not Feb 15, 2000 yet[/Then]
    [Else]The complex expression wasn't true[/Else]
[/If]

Comparisons are always case-insensitive so "grant" equals "GRANT". The expression is evaluated as a mathematical boolean equation, where each sub-expression evaluates to either 0 or 1 (meaning true or false). If the entire evaluated expression is true, then the WebDNA inside the [Then] contexxt is executed, otherwise the [Else] context is executed. The [Math] context has been extended to allow for quoted text and boolean operators, and is actually what is used by [If] to perform the work of evaluating the expression. A side-effect of this allows you to use these operators inside a [math] equation: [math]1<3[/math] evaluates to "1", because the equation is true. Conversely, [math]3<1[/math] evaluates to "0" because the equation is false. Similarly, [math]1&1[/math] evaluates to "1", and [math]1&0[/math] evaluates to "0".

Comparison Example

equal

= [If "[username]" = "SAGEHEN"] variable [username] is equal to SAGEHEN

not equal

! [If [random] ! 45] random number is not 45

contains

^ [If "[browsername]" ^ "Mozilla"] variable [browsername] contains the text Mozilla

begins with

~ [If "[ipaddress]" ~ "245.078.013"] variable [ipaddress] begins with 245.078.013

Notice the IP address has been typed with 3 digits in each portion of the address. This is very important for making these comparison work as expected.

less than

< [If [random] < 50] random number is less than 50

greater than

> [If [lastrandom] > 25] last random number is greater than 25
divisible by \ [If [index] \ 3] variable [index] is divisble by 3
or | [If (5>4) | (1<3)] Boolean comparison: if either side of the operator is true, then the comparison is true
and & [If (5>4) & (1<3)] Boolean comparison: if both sides of the operator are true, then the comparison is true
Delimiter   Example
Quoted Text
"..." [If "Hello" ^ "hell"] All text must be surrounded by quotes
Numbers   [If 12.5 < 13.2] Numbers do not need to be delimited; they function the same as in a [Math] context
Dates {} [If {[date]} > {9/7/1963}] Dates must be enclosed in curly braces to distinguish them from regular numbers
Times {} [If {[time]} > {12:31:00PM} Times must be enclosed in curly braces to distinguish them from regular numbers

Parentheses

(...) [If (3>1) & ("a"<"b")] You may collect groups of items in parentheses in order to force the order of evaluation

User Contributed Notes
IfContext  

Donovan - lists AT euca DOT us 
11/18/2003 at 12:23:11
A note:
When using some comparisons such as ">" or "<", make sure
you have a valid string or the statement will always issue the exception (ie [else][/else]).

example:
This won't work:
When "balance" = nothing
[text]balance=[/text]

[if ([balance]>0) | ("[balance]" = "")]
[then]
should do this
[/then]
[/if]

This will work:
When "balance" = nothing
[text]balance=[/text]

[if (0[balance]>0) | ("[balance]" = "")]
[then]
should do this
[/then]
[/if]

Because the first comparison now has something to compare, the statement is not "nulled" out and the second comparison is allowed to make the whole statement true.

Donovan


Brian Burton - brian@burtons.com 
02/16/2004 at 13:48:21
I'm posting this here so I don't have to find it everytime I forget this...

A really useful way of blanking out the value of a text variable if it wasn't set (for whatever the reason)

[showif [url][_email][/url]=%5B_email%5D][text]_email=[/text][/showif]

You HAVE to [url] your showif comparisons for ANY user enterable text to prevent user entered ampersands (&) and exclamation points (!) from improperly influencing the result.

This hint applies to the [hideif] context as well.

GJK - gary at garykrockover dot com 
04/27/2004 at 06:44:26
Nested IF statement to find the days/hours/minutes/seconds difference between two dates:


[text show=F]seconds_since=[math]((([math]{[date]}[/math])-1)*86400)+([math]{[time])[/math])[/math][/text]
[text show=F]old_seconds_since=[math]((([math]{[pdate]}[/math])-1)*86400)+([math]{[ptime])[/math])[/math][/text]

[text show=F]diff=[math][seconds_since]-[old_seconds_since][/math][/text]
[text show=f]diff=[diff][/text]
[math show=f]seconds=0[/math]
[math show=f]hours=0[/math]
[math show=f]minutes=0[/math]

[showif [math][diff]%86400[/math]<1]<!--there are 86,400 seconds in a day-->
[text show=f]days=[math][diff]/86400[/math][/text]
[/showif]

[showif [math][diff]%86400[/math]>0]
[text show=f]rest=[math][diff]%86400[/math][/text]
[text show=f]days=[math]([diff]-[rest])/86400[/math][/text]
[if ([math][rest]%3600[/math]>0)]
[then]
[text show=f]rest1=[math][rest]%3600[/math][/text]
[text show=f]hours=[math]([rest]-[rest1])/3600[/math][/text]
[if ([math][rest1]%60[/math]>0)]
[then]
[text show=f]rest2=[math][rest1]%60[/math][/text]
[text show=f]minutes=[math]([rest1]-[rest2])/60[/math][/text]
[text show=f]seconds=[rest2][/text]
[/then]
[else]
[text show=f]minutes=[math][rest1]/60[/math][/text]
[/else]
[/if]
[/then]
[else]
[text show=f]hours=[math][rest]/3600[/math][/text]
[/else]
[/if]
[/showif]

[hideif [days]=0][days] day[hideif [days]<2]s[/hideif], [/hideif][hideif [hours]=0][hours] hour[hideif [hours]<2]s[/hideif], [/hideif][minutes] minute[hideif [minutes]<2]s[/hideif], [seconds] second[hideif [seconds]<2]s[/hideif] ago

Copyright © 2008 WebDNA Software Corporation, Inc.