Free-Conversant Support / Grouping Conditionals
 Home   About Conversant   Free Sites   Hosting   Support   XML-RPC 

Search



Documentation >> Conditionals >> Advanced Features >> Grouping Conditionals

Grouping Conditionals

When we looked at Nesting Conditional Macros, we learned how to use more than one test to control what is displayed in part of a page. Later we looked at Complex Conditional Macros, which showed us how to use more than one conditional test in a single conditional macro, by combining them with "&&" (boolean AND) or "||" (boolean OR). Then in Negate Conditionals we learned how to tell Conversant to do the reverse the results of a test, using the "!" (NOT) operator. Now we're going to look at a new way to group our conditional macros in a single statement, to make sure we get the result we want.

Conditionals are grouped together with parentheses, and groups of parentheses are separated by the "&&" and "||" operators.

Groups of conditionals can be negated with the "!" operator.

A Tough Situation

Here's a hypothetical situation, that results in a difficult-to-write conditional. Let's assume that we want to modify a template so that the member's bookmarks macro is only used if the following conditions are true:

the user is a member of the group "sales"

AND

the user is a member of the group "marketing"

OR

the user is a member of the group "management"

We mean this to say "if the user is a member of both the sales and marketing groups, or is a member of the management team." How would you write this as a conditional macro? You might try to write it like this:

userGroupName == 'sales' && userGroupName == 'marketing' || userGroupName == 'management'

There are two problems with this conditional.

  1. It's very hard to understand it's intent. How do you know if it means "user must either be in both sales and marketing, or in management" or if it means "user must be in sales, and also in either marketing or management"?
  2. It doesn't work!

Grouping is the Solution

Conversant is designed to require us to write these really complex conditionals with groups (parentheses). Here's the same conditional with parentheses added (and formatting to control the display only, but you'd actually write it all on one line):

( userGroupName == 'sales' && userGroupName == 'marketing' ) || ( userGroupName == 'management' )

When reading a conditional macro with parentheses, remember one rule: whatever's inside the parentheses is always evaluated before whatever's outside. (Remember, that's how it works with math, too... so this shouldn't be a foreign concept.)

When Conversant processes this conditional, it breaks it down exactly as shown above.

Note: You can "negate" an entire grouped set of conditionals by putting a ! (negation operator) before the "(". See the next section for an example.

A Working Example

This really isn't complicated at all, as long as you remember that "groupings" (with parentheses) allow you to control the order that the conditions are processed. Let's finish up with a working example.

<!--#if condition="! ( requestIsEdit || requestIsReply ) && ( userAgent contains 'Mozilla/5.0' || userAgent contains 'MSIE 5.5' )"-->

1. You're looking at this page in a Mozilla or Internet Explorer 5.5 browser, and this page is not a message /edit or /reply page

<!--#else-->

2. Either this is an /edit or /reply page, or your browser is neither from the Mozilla project or Internet Explorer version 5.5

<!--#endif-->

Here's the above code in action:

2. Either this is an /edit or /reply page, or your browser is neither from the Mozilla project or Internet Explorer version 5.5

View in DG