Latest Register Log In

+ Advanced Search

Conditionals Syntax

Conditionals Syntax
By
10/09/03 (Edited 03/10/14)

Conditionals syntax allows you to make your templates dynamic without needing to learn php. The syntax is much like HTML. These tags consist of an opening <IF> and a closing </IF> just like HTML. You can specify conditions within the opening IF in order to determine when the text/html between the <IF> and </IF> will be shown.

Example:
<IF {THISMEMBERISADMIN}>
You are an admin.
</IF>

This works because the {THISMEMBERISADMIN} template variable has a true/false value. For more generic true/false testing you can use the BOOL template variable appendage. For example if you want to test whether the link has a description, and if it does display it, use <IF {LINKDESCRIPTIONBOOL}>Here's the decription: {LINKDESCRIPTION}</IF>.

For more complex conditionals, try these paramaters:

is: <IF {THISMEMBERNAME} is Paul>Hi Paul</IF>
is not: <IF {THISMEMBERUSERGROUP} is not 2>You are not in usergroup #2 (the regular member group)</IF>
is greater than: <IF {THISMEMBERLINKS} is greater than 10>Congrats, you've submitted more than 10 links.</IF>
is less than: <IF {THISMEMBERLINKS} is less than 10>You've submitted less than 10 links... come on, you must know more good sites than that.</IF>
contains: <IF {THISMEMBERNAME} contains e>There is an "e" somewhere in your username.</IF>

Note that quotes are in most cases optional, as the script detects where they ought to go an inserts them for you, but for a non-numeric value it's safer to put them in. Numeric values should not be quoted, particularly when comparing with is greater than or is less than. If you get confused, just leave off quotes for everything.

You can make more complex conditions using 'and' and 'or'. There is no limit to how many terms you can use. For example:

<IF {THISMEMBERNAME} is not Paul and {THISMEMBERLINKS} is greater than 3>You aren't Paul and you have more than 3 submitted links.</IF>

<IF {THISMEMBERNAME} is not Paul or {THISMEMBERLINKS} is greater than 3>You aren't Paul <b>or</b> you are Paul but have more than 3 submitted links.</IF>

Please note that text comparisons are dangerous, because if the text (usernames in the above example) ever contains operators it'll break the page. To be safe with unknown text, you can use a special FIELDCONTAINS check like this: {LINKFIELDCONTAINS[fieldname <,> text here]}. In the above example for the viewing member's username, it'd be {THISMEMBERFIELDCONTAINS[name <,> Paul]}.

Alternatively, if you're just checking whether there's any text entered and not what the text is, use the BOOL} version of the template variable: <IF {LINKCITYBOOL}>there's a city entered named {LINKCITY}</IF>



Often, you either want to show one bit of text or another. Instead of the clunky method of using two different IFs to cover it, you can use <OTHERWISE> (or <ELSE>, which works the same). Use as in this example:

<IF {THISMEMBERUSERGROUP} is greater than 1>
You are a registered member.
<OTHERWISE>
You are not registered or not loggedin, please register or login now.
</IF>

If you're familiar with case syntax, you can also do that with conditionals:
<IF {THISMEMBERUSERGROUP} is 1>
You are a guest. Please register.
<OTHERWISE IF {THISMEMBERGROUP} is 2>
You are a regular registered member.
<OTHERWISE IF {THISMEMBERGROUP} is 3>
You are an admin.
<OTHERWISE>
You are in a custom usergroup of some sort.
</IF>

Note that ELSE works in place of OTHERWISE for those who prefer it.

The template variables available for your use in conditionals are listed here.

The possibilities for using this syntax with template variables are limitless. Be creative.

Note that template conditionals can be used in language items as well as templates.

Troubleshooting

If you have a listing field where the contents can include WSN codes or HTML, testing with <IF {LINKFIELD}>{LINKFIELD}</IF> will display improperly. This is because it sees the contents as HTML, and sees an > which is inside that HTML as being the closing > of the condition. You can work around this by testing with a special BOOL version of the template variable which simply returns true if there's something there and false if there isn't: <IF {LINKFIELDBOOL}>{LINKFIELD}</IF>

Especially complex conditionals may break such that you simply need to use template PHP instead.




Description Simple ways to make your templates dynamic without learning php.
Rating
Views 6170 views. Averaging 1 view per day.