We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9207 ☆ A M B ☆
    • 2,475 Posts
    Maybe this just shows how inexperienced I am (or how little sleep I’ve gotten), but I’m running into some odd behavior as I move one of my Snippets from the database and into a file. There’s still a MODx Snippet, but most of it has been replaced with an include statement that references the new file in /assets/snippets/newfolder/mysnippet.php

    Look at the two variations of the same line (used to debug a SQL query):

    if ( isset($debug) )        { print $query_str; exit; }; 
    if ( isset($debug) )        { return $query_str; exit; };  // This one only works if it's in the database, not in the file.


    There’s nothing special in my include file... no functions are being called from the PHP code stored in the database. Is that why the "return" statement doesn’t work? I suspect that it would if I restructured my code so that the PHP code in the database called a function in the file. That would be a better way to do it... but I’m curious as to the explanation for the behavior here. Sorry if it’s really obvious, I just don’t follow what’s going on. Thanks!
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      A snippet IS a function (the snippet code is fed to the PHP eval() function). Including a file in eval() code can have unexpected results. One of the first things Revolution development did was to get rid of that eval() of snippet code.
        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
        • 3749
        • 24,544 Posts
        Quote from: Everett at Apr 28, 2009, 01:25 PM

        Maybe this just shows how inexperienced I am (or how little sleep I’ve gotten), but I’m running into some odd behavior as I move one of my Snippets from the database and into a file. There’s still a MODx Snippet, but most of it has been replaced with an include statement that references the new file in /assets/snippets/newfolder/mysnippet.php

        Look at the two variations of the same line (used to debug a SQL query):

        if ( isset($debug) )        { print $query_str; exit; }; 
        if ( isset($debug) )        { return $query_str; exit; };  // This one only works if it's in the database, not in the file.


        There’s nothing special in my include file... no functions are being called from the PHP code stored in the database. Is that why the "return" statement doesn’t work? I suspect that it would if I restructured my code so that the PHP code in the database called a function in the file. That would be a better way to do it... but I’m curious as to the explanation for the behavior here. Sorry if it’s really obvious, I just don’t follow what’s going on. Thanks!

        I’m short of sleep also, but I think the first one will work if you change your "include" statement to "return include";
          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
          • 10449
          • 956 Posts
          if return doesn’t work, just use echo()
            • 9207 ☆ A M B ☆
            • 2,475 Posts
            Yeah, I can use print or echo... I just wanted to keep in line with the function mentality and return a value.

            How is Revolution handling Snippets if not via an eval()?
              • 9207 ☆ A M B ☆
              • 2,475 Posts
              More problems with included files in Snippets... if I call the Snippet twice on the same page, it dies. The first call executes, the second does not. And oddly, if i use "include()", the page dies.... it won’t load at all. If I use "include_once()" it loads the page, but only the first instance is resolved. I think this actually makes sense, but I’d appreciate it if someone else wiser than me explained it.

              Also... any tips on coding practices or pit falls when writing Snippets that reference external files?

              I’ve put the code back in the database, but it REALLY doesn’t like having functions in there... if there’s a function in the Snippet, it dies.

              Thanks!
                • 10449
                • 956 Posts
                  • 9207 ☆ A M B ☆
                  • 2,475 Posts
                  Nice. Thank you... I’ve read through that article many times, but I guess that bit about "ifexists()" just went in one ear and out the other.