What is If?
A logical conditional snippet that allows for conditionals in MODx.
If was written by Jason Coward (opengeek) and Shaun McCormick (splittingred) and first released on Oct 29th, 2009.
Development and Bug Reporting
If is stored and developed in GitHub, and can be found here:http://github.com/splittingred/If
Bugs can be filed here: http://github.com/splittingred/If/issues
The If snippet can be called using the tags:
or, in case your subject can be modified before the resource cache get cleared use the uncached syntax:
|subject||The value to perform the conditional on.|
|operator||The operator to compare the subject against.||=|
|operand||If needed, the value to compare the subject against using the operator.|
|then||If the condition is true, output this.|
|else||If the condition is false, output this.|
|debug||If true, print out all the passed properties.||0|
|die||If debug and this is true, die() after printing the properties.||0|
|!=,neq,not,isnot,isnt,unequal,notequal||Passes if the subject is unequal to the operand.|
|==,=,eq,is,equal,equals,equalto||Passes if the subject is equal to the operand.|
|<,lt,less,lessthan||Passes if the subject is less than the operand.|
|>,gt,greater,greaterthan||Passes if the subject is greater than the operand.|
|<=,lte,lessthanequals,lessthanorequalto||Passes if the subject is less than or equal to the operand.|
|>=,gte,greaterthanequals,greaterthanequalto||Passes if the subject is greater than or equal to the operand.|
|isempty,empty||Passes if the subject is empty.|
|!empty,notempty,isnotempty||Passes if the subject is not empty.|
|isnull,null||Passes if the subject is null.|
|inarray,in_array,ia||Passes if the subject is found in the operand list. (comma-delimited string)
[[!If? &subject=`[[+total]]` &operator=`GT` &operand=`3` &then=`You have more than 3 items!`]]
[[!If? &subject=`[[+name]]` &operator=`EQ` &operand=`George` &then=`Hey George! Long time no see!` &else=`You're not George. Go away.` ]]
Inline snippet calls:
[[!If? &subject=`[[+modx.user.id]]` &operator=`EQ` &operand=`0` &then=`[[Login]]` &else=`[[Logout]]` ]]
When using the If snippet for checking a resource field or template variable (or other value that doesn't change before the cache does), be sure to use the cached snippet call to check the condition to make sure it doesn't need to process on every request.
[[If? &subject=`[[*hidemenu]]` &operator=`EQ` &operand=`1` &then=`This resource is not visible in the menu.` &else=`This resource shows up in the menu in spot [[*menuindex]].` ]]
Example with in_array:
[[If? &subject=`[[*id]]` &operator=`inarray` &operand=`3,4` &then=`This text will show if id is 3 or 4.` &else=`This text is printed for all other resource id's.` ]]
Suggest an edit to this page.