• AndIf - conditional snippet allowing multiple AND/OR conditions#

  • Lucas Reply #1, 1 year, 1 month ago

    Reply
    AndIf - for MODx Revolution
    AndIf is an 'If...then...else' snippet that accepts multiple AND/OR conditions.
    It's based on the existing 'If' snippet by splittingred and OpenGeek.
    I'm still testing and am hoping for some constructive feedback here
    Cheers,
    Luke

    The Snippet
    Snippet code is attached to this post. Call it like this:
    [[!AndIf? 
       &condition=``   <-- condition(s) to test  (see description below)
       &operator=``    <-- AND or OR (defaults to AND)
       &then=``        <-- result if conditions met  (defaults to 'true')
       &else=``        <-- result if not  (defaults to 'false')
    ]]
    


    Property Descriptions
    &condition=``
       
    • Each condition is of the format subject operator operand, separated with one space ' '
            eg. &condition=`5 < 6`    (returns true)
    •  
    • Separate multiple conditions with a pipe '|'
    •  
    • Tested up to 10 conditions, worked fine
    •  
    • Makes use of all conditional operators supported by If
    eg.
    Single condition:      &condition=`[[*parent]] isnot 2`
    Multiple conditions:   &condition=`[[*template]] eq 5|[[*image]] isempty 1`
    


    Note: For in_array operator, ensure array is input as a comma ',' delimited string
       
    • eg. &condition=`5 in_array 4,5,6`    (returns true)


    &operator=`` Global operator to be applied to all conditions provided.
       
    • Accepts 'AND' or 'OR'  (defaults to 'AND')
    •  
    • eg. IF condition1 AND condition2 ARE BOTH TRUE, OUTPUT TRUE
            IF EITHER condition1 OR condition2 ARE TRUE, OUTPUT TRUE


  • Letti Reply #2, 1 year, 1 month ago

    Reply
    Very good idea. Thanks a lot for this Snippet...


  • Lucas Reply #3, 1 year ago

    Reply
    No problem Actually, yours was one of the posts I saw enquiring about this functionality so I decided to build it into a snippet.

    Is it working ok for you?


  • vegadesign Reply #4, 1 year ago

    Reply
    Hello, Luc!
    Nice snippet.
    Could I do somthing like this?
    I have a template. In one place, depending of the page ID, I would some chunks.
    For example:
    [[!If? &subject=`` &operator=`=` &operand=`3` &then=`` OR &subject=`` &operator=`=` &operand=`4` &then=``]]
    ie more then one conditions in one place.




  • Lucas Reply #5, 1 year ago

    Reply
    This will allow more than one condition but works like 'If all conditions are met, do this; If not, do that...'
    While what you want is: 'If condition 1, do this; If condition 2, do that... etc'

    I think the Switch snippet will do that


  • BearsFan Reply #6, 7 months ago

    Reply
    Hi, sorry, I'm not seeing the attachment for this snippet? What am I missing? Thanks
    Anthony