We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36760
    • 136 Posts
    I'm working with the AndIf snippet from this thread: http://forums.modx.com/thread/32601/andif---conditional-snippet-allowing-multiple-and-or-conditions (Github: https://gist.github.com/mkay/8480032) on Modx Revolution 2.2.4-pl. I'm attempting to use the following code to set the width of sidebars depending on which ones have content.

    [[!AndIf? &condition=`[[*leftSideContent]] notempty|[[*rightSideContent]] empty` &operator=`AND` &then=`eight` &else=`else`]]


    The problem is even when leftSideContent has content and rightSideContent has no content it always displays "else." I've tried every variation of notempty that's included in the snippet (!empty, isnotempty, notempty, and empty 0). I've also tested that setting both to "empty" works properly. Finally, I have the normal If addon installed and working, even though I think the AndIf snippet is a standalone.

    Is this an issue with the snippet or the Modx version? I know the snippet is quite old, but it seems to work well besides this issue.

    Thanks for the help!

    This question has been answered by BobRay. See the first response.

      • 3749
      • 24,544 Posts
      That seems like a convoluted and slow way to get what you want (and maybe beyond the parsing ability of the AndIf snippet).

      Can you clarify what you want returned in each of these conditions?

      Left empty + right empty
      Left empty + right not empty
      Left not empty + right not empty
      Left not empty + right empty
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 36760
        • 136 Posts
        Those conditions are correct.

        I did think about just writing my own snippet specifically for this. That way I have one snippet call instead of many. Is that what you had in mind as being a better solution?
          • 36760
          • 136 Posts
          Okay, it was even easier than expected. Here's what I used:

          Snippet titled sidebarWidths:
          if(!empty($left) && empty($right)){
              return 'eight';
          }elseif(empty($left) && !empty($right)){
              return 'nine';
          }elseif(!empty($left) && !empty($right)){
              return 'five';
          }else{
              return 'twelve';
          }


          Snippet call:
          [[!sidebarWidths? &left=`[[*leftSideContent]]` &right=`[[*rightSideContent]]`]]


          Let me know if you think there's an even better way.
          • discuss.answer
            • 3749
            • 24,544 Posts
            It looks fine, though it should probably have only one return statement. Just change the returns to $output =, and add return $output at the very end.

            I think I might do it this way (slightly more efficient), but your way is somewhat easier to follow:

            if (empty($left)) {
                $output = empty($right) ?  'twelve' : 'nine';
            } else {
                $output = empty($right) ? 'eight' : 'five';
            }
            
            return $output;


            In theory, you could do it with a single line using nested ternary operators, but it would be almost impossible to read. wink
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting