We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33968
    • 863 Posts
    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 smiley
    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 tongue
    •  
    • 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

    UPDATE: Please download the second, updated file shown below (forum won't allow me to remove the first) [ed. note: okyanet last edited this post 11 years, 3 months ago.]
      • 17016
      • 138 Posts
      Very good idea. Thanks a lot for this Snippet...
        • 33968
        • 863 Posts
        No problem smiley 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?
          • 31378
          • 51 Posts
          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=`[[*id]]` &operator=`=` &operand=`3` &then=`[[$chunk-1]]` OR &subject=`[[*id]]` &operator=`=` &operand=`4` &then=`[[$chunk-2]]`]]
          ie more then one conditions in one place.


            • 33968
            • 863 Posts
            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 smiley
              • 37143
              • 74 Posts
              Hi, sorry, I'm not seeing the attachment for this snippet? What am I missing? Thanks
              Anthony
                • 16430
                • 217 Posts
                Quote from: [email protected] at Oct 21, 2011, 05:49 PM
                Hi, sorry, I'm not seeing the attachment for this snippet? What am I missing? Thanks
                Anthony
                Login
                • This looks cool. be sure to share on Twitter too to get feedback if you haven't already.
                    Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. BlogTwitterLinkedInGitHub
                    • 27117
                    • 28 Posts
                    Just found this wonderful snippet and if solved my problem in seconds. Thank you very much for this!
                      Modx 2.1.3, PHP 5.2
                      • 37358
                      • 1 Posts
                      Really useful snippet. Thanks so much!