We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 35176
    • 193 Posts
    I’ve seen the MODx "run" command, to execute a snippet in another snippet (i think?)

    But is that what i would use for an "include" statement in a snippet?

    so the equivalent of:
    include "./rss_export.php";


    would be?
    include $modx->runSnippet($rss_export);


    or, can i say?
    include [[!rss_export]]
      jealousy<>love
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Snippet code is ordinary PHP code; include is include (includes the specified file), as is include_once, require and require_once.

      Since snippet code is passed to PHP one way or another, MODx tags don't work (the PHP parser is not the MODx parser and doesn't understand MODx tags, and vice versa), so the API function $modx->runSnippet('snippetName', array('param1'->'value1','param2'->'value2',...)) is used to run a snippet from within a snippet.

      You may find this useful; although it applies to Evo the principles are much the same.

      http://sottwell.pogwatch.com/how-snippets-work.html [ed. note: sottwell last edited this post 10 years, 11 months ago.]
        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
        • 35176
        • 193 Posts
        i’m getting an error with runSnippet.
        Fatal error: Class 'rss_export' not found in /home/bayviewf/public_html/modx2/core/cache/elements/modsnippet/6.include.cache.php on line 52


        i have a snippet called rss_export.

        i’m including it in snippet feed2html with:
        // include "./rss_export.php";
           include $modx->runSnippet($rss_export);


        i’m calling feed2html in my html page with:
        [[!feed2html]]


        why is runSnippet looking for the snippet in a cache? how can i ensure it’s loading the non-cached snippet?

        Snippet code is ordinary PHP code; include is include (includes the specified file), as is include_once, require and require_once.
        j
        then i should be able to say:
        include [[!rss_export]]

        but that returns:
        Parse error: syntax error, unexpected '[' in /home/bayviewf/public_html/modx2/core/cache/elements/modsnippet/6.include.cache.php on line 20


        any ideas?
        Thanks!
          jealousy<>love
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          It’s not a variable, it’s a parameter to the runSnippet function:
          $modx->runSnippet('rss_export');

          This is presuming you have a snippet named "rss_export". It’s not a .php file, it’s a snippet in the database.
            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
            • 35176
            • 193 Posts
            i have a snippet called rss_export.
              jealousy<>love
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              So your calling snippet should look like this:
              $rss_export = $modx->runSnippet('rss_export');
              // do whatever with the results of running the rss_export snippet code
              // and gather its output into a variable
              return $output;
                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
                • 35176
                • 193 Posts
                I’m trying to adapt existing php code. I believe it includes the external file, and then uses classes and functions from that external file one or more times.

                If im understanding you, Your example would certainly complicate things and create more effort, if I have to manually code that for every function call, instead of a single include statement, or something like it.

                I would make it an actual include statement, but MODx prohibits uploads of php files. In fact I’d prefer make the main php script a standalone file, then I would not have to make any changes. But MODx does not allow.
                  jealousy<>love
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  Then all you need to do is include your .php file and instantiate the object.
                  include 'path/to/myclass.file.php';
                  $obj = new MyObject;
                  

                  Of course, your snippet should check for the existence of the file and the object to catch any errors if for some reason the file can’t be included.

                  Consider the Jot snippet. The snippet code starts with
                  $jotPath = $modx->config['base_path'] . 'assets/snippets/jot/';
                  include_once($jotPath.'jot.class.inc.php');
                  
                  $Jot = new CJot;
                  

                  then the rest of the snippet’s code consists of setting up the object’s settings based on the parameters used in the Jot snippet tags before finally calling object’s main function, returning its output.
                  $Jot->VersionCheck("1.1.4");
                  $Jot->Set("path",$jotPath);
                  $Jot->Set("action", $action);
                  $Jot->Set("postdelay", $postdelay);
                  $Jot->Set("docid", $docid);
                  $Jot->Set("tagid", $tagid);
                  ...
                  return $Jot->Run();
                  

                    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
                    • 35176
                    • 193 Posts
                    But MODx does not allow php file uploads, and it does not have php static types. I tried to make one, didn’t work. I’d rather make the parent snippet a php file too.
                      jealousy<>love
                      • 28042 ☆ A M B ☆
                      • 24,524 Posts
                      You are not paying attention. You have a .php file. You want to include it. So, in a snippet, use the include directive. That’s all I can say.
                        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