We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 13226
    • 953 Posts
    Hi All

    Tried updating my major site today and gave up due to major problems with Snippets no longer working properly.

    Looking at this post "garryn" says that snippet processing hasn’t changed from 1.0.2 to 1.0.3.

    Is this also the case for 1.0.0 to 1.0.3 ??

    A quick example of what no-longer renders correctly:

    <?php
    ?>
    Content Here with <?php ?> tags all over the place
    <?php
    ?>
    


    I’m not a phper myself - I’ve had all the coding done by a professional developer

    I’ve also had the majority of other problems that are mentioned here in the 1.0.3 Release Support.

    Have been using MODx since its start, never had problems like this before sad
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      I’ve never thought that could work, but it actually does. Learn something new every day!

      <?php
      $output = "<h2>From the Snippet</h2><p>This header and paragraph are from the snippet.</p>";
      ?>
      <p>This paragraph is from HTML in the snippet.</p>
      <?php
      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
        • 13226
        • 953 Posts
        Sorry, that structure has never worked in snippets

        Hi Susan,

        It does work smiley Ive been using this form of coding over a year now.

        I have multiple snippets that use this structure and work perfectly in 1.0.0 and a few older versions.
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Hmm. It looks like it has to be a certain structure to work.

          This one works as expected
          <?php
          $output = "<p>This  paragraph is from PHP in the the snippet.</p>";
          ?>
          <p>This paragraph is from HTML in the snippet.</p>
          <?php
          $output .= "<p>And this paragraph is also from the PHP in the snippet.</p>";
          return $output;
          ?>

          This one also works
          <?php
          $output = "<p>This  paragraph is from PHP in the the snippet.</p>";
          ?>
          <p>This paragraph is from HTML in the snippet.</p>
          <?php
          return $output;
          ?>

          This structure gets converted when saved
          <?php $output = "<p>This  paragraph is from PHP in the the snippet.</p>"; ?>
          <p>This paragraph is from HTML in the snippet.</p>
          <?php return $output; ?>

          Into this
          <?php
          $output = "<p>This  paragraph is from PHP in the the snippet.</p>"; ?>
          <p>This paragraph is from HTML in the snippet.</p>
          <?php return $output;
          ?>

          which echos the HTML and the last return statement, but not the $output:
          This paragraph is from HTML in the snippet.
          return $output;
          This is the same behavior as in version 1.0.2. However, an old unused site I have on 0.9.2 does the same thing on saving, but the code works as expected.
            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
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            Here is the snippet in the database in all versions:
            $output = "<p>This  paragraph is from PHP in the the snippet.</p>"; ?>
            <p>This paragraph is from HTML in the snippet.</p>
            <?php 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
              • 13226
              • 953 Posts
              Thanks for your time Susan.

              Ive just sent a mail to my developer, hopefully we can get something sorted.

              Cheers
                • 31178
                • 128 Posts
                There did seem to be some changes in the way snippets are handled from 1.0.2 as I logged something similar back when 1.0.2 came out:

                http://svn.modxcms.com/jira/browse/MODX-1459
                  • 21257 MODX Staff
                  • 730 Posts
                  Snippets in Evo are eval’d php code, and that php code is expected to return a value via php’s return function.
                  Any other behavior (e.g. the snippet code outputs content to the output buffer) will have different results based on the circumstances of the snippet call. I would definitely not recommend any developer rely on such behavior unless they’re doing a lot of unit testing and code tracing, and even then I can’t think of a good use case that would justify it.
                    Mike Schell
                    Lead Developer, MODX Cloud
                    Email: [email protected]
                    GitHub: https://github.com/netProphET/
                    Twitter: @mkschell
                    • 3749
                    • 24,544 Posts
                    Mixing code and HTML output in this way is always a risky business and will tend to bite you eventually in unpleasant and unexpected ways. If the output appears where you want it to relative to the value returned by the snippet, there’s no guarantee that it will continue doing so reliably.

                    The best practice is to put everything you want displayed into the snippet’s return value (or a set or placeholders with $modx->setPlaceholder() ), and never break out of PHP mode.

                    $output = "<p>This  paragraph is from PHP in the the snippet.</p>";
                    $output .= '<p>More output from  the snippet.</p>';
                     return $output; 




                      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
                      • 28042 ☆ A M B ☆
                      • 24,524 Posts
                      I suppose that’s why I just presumed it didn’t work that way; it’s never occurred to me to do it.
                        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