We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17125
    • 11 Posts
    Any idea on how to debug a Snippet.
    When a snippet does not work, it just shows a blank screen.
    Currently I have a call to snippet(with proper spaces of course) which just renders a blank page.
    Is there a way to find the line in the snippet that fails resulting in the blank screen.

    Is there something in documentation indicating how to produce error messages etc instead of blank page for snippet calls?
    May be it’s very basic but than I have missed it.

    This is a very common recurring problem.
    Would be glad to get pointers/ideas for this.
      • 10449
      • 956 Posts
      Does it work outside the modx framework?

      Maybe post it here and someone might find a clue...
        • 3749
        • 24,544 Posts
        Blank output often means a fatal error in the PHP code of the snippet or (less often) invalid HTML produced by the snippet. If it’s a PHP error, it’s most often a missing brace, semicolon, quotation mark, mistyped comment open or close, or unmatched parentheses.

        Here’s code with a couple of common errors:

        <?php
        
        $x=1;
        $y=2;
        $z=3;
        
        if ($x == ($y + $Z) {
           doThis();
        }
        ?>
        

        If you have a code editor with syntax highlighting, it can help you find these errors. There’s a MODx plugin to add syntax highlighting in the Manager http://modxcms.com/GeSHi-Syntax-Highlighter-0.4-0.html, I haven’t used it but people seem to like it. I don’t know if it works with the current version of MODx.

        Another way to find an error (if it’s not obvious) is to go here: http://www.icosaedro.it/phplint/phplint-on-line.html. Paste the snippet code into the upper window, turn off everything but errors (e.g. turn off warnings and notices), and click on the "parse" button.

        If there’s a fatal error in your PHP code, the report below will end just after that error. The PHPLint error message isn’t always helpful, but at least you know where to look for the error.

        Here’s the PHPLint report for the code above:

        1: <?php
        2:
        3: $x=1;
        4: $y=2;
        5: $z=3;
        6:
        7: if ($x == ($y + $Z) {

        if ($x == ($y + $Z) {
        \_ HERE
        **** 7: ERROR: variable `$Z’ has not been assigned

        if ($x == ($y + $Z) {
        \_ HERE
        **** 7: FATAL ERROR: expected closing ’)’ after ’if’ condition, found symbol sym_lbrace

        Creating a PHPLint plugin (or Module) for MODx is left as an exercise for the reader. grin



          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
          • 17125
          • 11 Posts
          Thanks a lot BobRay,

          The PHPlint links seems to be quite useful and comprehensive.
          You are right about the fact about syntax errors resulting in blank page.
          But definitely helps to get line numbers of such errors with this link.



            • 3749
            • 24,544 Posts
            PHPLint can also train you out of some questionable coding practices if you turn warnings on. If you use it regularly, you unconsciously begin to avoid things that generate warnings and your error rate goes down significantly.

            Bob
              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