We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28673
    • 178 Posts
    Hi! Since in a snippet I must return something, what if I want, say, analyse an object with print_r or similar functions?
      • 22303 MODX Staff
      • 10,725 Posts
      return print_r($variable, true);
        • 28673
        • 178 Posts
        :)
          • 28673
          • 178 Posts
          And how to get some output say, from a function or a loop?
            • 4172
            • 5,888 Posts
            example?
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 28673
              • 178 Posts
              example?

              well, say I’m in a snippet, and I’ve got a function:

              function test($var){
              //here I want to output the var in the browser so I can check it
              //regular debug..
              }

              The same if I for example want to check what a loop is doing (for, while, array_walk, etc..)

                • 4172
                • 5,888 Posts
                I’m not sure what’s the prob,

                you can directly output them as allways with print_r or echo or var_dump
                or you can log them to error-log
                or collect them into your returned output-string
                or collect them in placeholders
                or...........
                  -------------------------------

                  you can buy me a beer, if you like MIGX

                  http://webcmsolutions.de/migx.html

                  Thanks!
                  • 28673
                  • 178 Posts
                  Pardon, I didn’t understand that. I was sure I could only have output from the final return and never ever could use echo print_r etc..
                    • 22303 MODX Staff
                    • 10,725 Posts
                    Quote from: stratboy at Oct 26, 2010, 11:56 AM

                    Pardon, I didn’t understand that. I was sure I could only have output from the final return and never ever could use echo print_r etc..
                    First, are you working in MODx 1.0 (Evolution) or MODx 2.0 (Revolution)?

                    Regardless, the echo’d content will be prepended to the output if execution of the Snippet completes successfully, i.e. you could collect output in an array...
                    <?php
                    $output = array();
                    foreach ($collection as $itemkey => $item) {
                        $output[] = print_r($item, true);
                    }
                    return implode("\n", $output);
                    ?>

                    Or if using Revolution, you can use the log() function:
                    <?php
                    $oldTarget = $modx->setLogTarget('HTML');
                    $output = '';
                    // opening code to get a collection of objects here...
                    $collection = $modx->getCollection('modUser', array('active' = true));
                    foreach ($collection as $key => $object) {
                        // normal code to set output here...
                        $data = $object->toArray();
                        $output = $modx->getChunk('myUserTpl', $data);
                    
                        // now output some debugging info to the page
                        $modx->log(modX::LOG_LEVEL_ERROR, "object {$key} = " . print_r($data, true));
                    }
                    // do more stuff here
                    $modx->setLogTarget($oldTarget);
                    return $output;
                    ?>
                      • 28673
                      • 178 Posts
                      Hi, I’m using Revo, as you can see in the title of the thread.. Thank you very much for your advices!