We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17578
    • 33 Posts
    Hi All.

    I'm trying to track down some errors I'm getting from a snippet that gets the height and width of an image using getimagesize();. I run the snipped inside an IF statement that checks there's actually an image in the TV field;

    [[!If? &subject=`[[+icon]]` &operator=`notempty` &then=`[[!mySnippet]]" &else=``]]

    But for every field that does not have an image I get the error; "... warning: getimagesize(): Filename cannot be empty" These errors pile up and slow down page loads.

    The error seems weird because the snippet should not execute, but it is.

    [[+icon]] is part of a MIGX TV so using a Chunk after then= isn't an option.

    Is it possible to check for an empty field in an if / else statement in the snippet? Any suggestions would be welcome.

    Thanks!

    My config is: MODX Revolution 2.3
      • 3749
      • 24,544 Posts
      MODX runs your snippet when it parses the tag.

      This might work:

      [[[[!If? &subject=`[[+icon]]` &operator=`notempty` &then=`!mySnippet" &else=``]]]]


      But why not just see if the icon is empty in your own snippet and return '' if it is? That would be more efficient.

      [[!MySnippet? &icon=`[[+icon]]`]]



      $icon = $modx->getOption('icon', $scriptProperties, '');
      
      if (empty($icon)) {
          return '';
      }
      
      /* The rest of your snippet here */



        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
        • 17578
        • 33 Posts
        Hey BobRay!

        Of course the second option worked. As always, thanks.

        I use my original snippet in a lot of places so I was trying to write the IF statement globally so that any empty field would not run the script. But I don't think that's possible, right?

        Again, thanks,

        Rob
          • 3749
          • 24,544 Posts
          Did you try the first option? It shouldn't run the script unless the icon tag is not 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
            • 17578
            • 33 Posts
            I did try the first option and still saw the errors, weird.
              • 3749
              • 24,544 Posts
              The second option will be faster anyway. 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