We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3177
    • 137 Posts
    Hello,

    I have a snippet which should do something only if a certain chunk has any content.
    Simply put:
    <?php
    $xx = "{{mychunk}}";
      if ($xx == "") {
        print "Chunk is empty, no action";
      }
      else {
        print "Chunk has content, do your thing...";
      }
    ?>
    


    This does not work. Even using other PHP tests like strlen or empty functions.
    They all jump to the ’else’-branch regardless if the chunk has any content.

    My guess is this has something to do with the order of executing
    MODX tags and PHP code.

    But, the question is therefore: How do I test the content of a chunk in a snippet?

    Thanks for your answers.

    Bert
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      You can’t use MODx tags in php code. You’ll need to use the API functions; in this case $modx->getChunk(’chunkName’).
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 13577
        • 302 Posts
        Untested. But you might try using the getChunk API method:

        <?php
        $xx = $modx->getChunk('myChunkName');
          if ($xx == "") {
            print "Chunk is empty, no action";
          }
          else {
            print "Chunk has content, do your thing...";
          }
        ?>
        


        Darn Susan - you were too fast for me!! wink
          Standard Disclaimer
          I could be totally wrong.
          • 3177
          • 137 Posts
          Yes!!!
          Thanks people for your quick response.