We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 32070
    • 87 Posts
    I’ve got a number of snippets that use blocks of escaped HTML and inline php i.e.
    <?php
    ...some php code...
    ?>
    
    <a href="<?php echo $link; ?>">link text</a>
    
    <?php
    ...some more php code...
    ?>
    


    MODx 1.0.1 kills ANY snippet that I have like this. Only way I can get it to work is if I rewrite with <?php on a new line

    
    <a href="
    <?php
    echo $link;
    ?>">link text</a>
    
    


    I’ve also been able to call some snippets from inside a snippet in the past . . . 1.0.1 doesn’t let me do this either . . . meaning that a bunch of my stuff isn’t working in 1.0.1.

    Thoughts?
      Des
      webandflowdesign.com
      • 3749
      • 24,544 Posts
      You can’t break out of PHP in a MODx snippet. The usual method is:

      <?php
      
      $output = 'HTML CODE';
      
      // php code
      
      $output .= 'MORE HTML CODE';
      
      //php code
      
      return $output;
      ?>


      To execute another snippet, you need to use:

      $modx->runSnippet()


      http://wiki.modxcms.com/index.php/API:runSnippet
        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
        • 32070
        • 87 Posts
        Is this a situation where the practice was discouraged in the past and is finally being enforced in 1.0.1? I definitely have snippets that break out of PHP and work just fine under 1.0.0 and previous. Obviously I’ll rewrite my snippets the CORRECT way, but I’ve been doing it because it worked.

        Thanks for the help.
          Des
          webandflowdesign.com
          • 9130
          • 171 Posts
          Quote from: webandflow at Oct 27, 2009, 06:24 AM

          Is this a situation where the practice was discouraged in the past and is finally being enforced in 1.0.1? I definitely have snippets that break out of PHP and work just fine under 1.0.0 and previous. Obviously I’ll rewrite my snippets the CORRECT way, but I’ve been doing it because it worked.
          Something like that. I don’t think this was officially supported in earlier versions as well. I’m surprised it actually worked for you.

          The ’correct’ way is to separate the code from the html completely. Move the html into a chunk and have the snippet use that chunk for its output:
          // Use a chunk, and within this chunk, use placeholders to output any dynamic data.
          // What we're doing here is first to set the placeholders, and then we're returning the chunk with the edited placeholders.
           
          // Set placeholders:
          $modx->setPlaceholder('link', $linkUrl);		
          $modx->setPlaceholder('lastvisit', $varLastvisit);
           
          // Return the chunk
          return $modx->getChunk('NameOfChunk');

          http://wiki.modxcms.com/index.php/Creating_Snippets#Snippets_and_plugins_should_not_contain_presentation_layer_code
            • 4172
            • 5,888 Posts
            Or use the api-function parseChunk:

            // Set placeholders-array:
            $ph_array['linkUrl']='yourlinkurlstring';
            $ph_array['lastvisit']='yourlastvisitvalue';
            
            return $modx->parseChunk('nameOfChunk', $ph_array, '[+', '+]');
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 21257 MODX Staff
              • 730 Posts
              Quote from: webandflow at Oct 26, 2009, 11:14 PM

              I’ve got a number of snippets that use blocks of escaped HTML and inline php i.e.
              <?php
              ...some php code...
              ?>
              
              <a href="<?php echo $link; ?>">link text</a>
              
              <?php
              ...some more php code...
              ?>
              


              If you have an example of a snippet like this that worked before 1.0.1 I’d love to take a look.

              You can definitely include php files that mix php and html blocks, but even then, since snippets rely on a return value, it would make sense to use an output buffer to capture any echoed output from such an include.
                Mike Schell
                Lead Developer, MODX Cloud
                Email: [email protected]
                GitHub: https://github.com/netProphET/
                Twitter: @mkschell
                • 32070
                • 87 Posts
                Quote from: netProphET at Oct 27, 2009, 08:04 AM

                If you have an example of a snippet like this that worked before 1.0.1 I’d love to take a look.

                I emailed you three examples of snippets that are currently working in 1.0.0, rather than post them here. I’ll be spending the next day or so reviewing all of my custom snippets and rewriting them so they’re more "official"

                Thanks.
                  Des
                  webandflowdesign.com
                  • 13986
                  • 90 Posts
                  I ran into some troubles as well with a snippet that worked fine in Evo 1.0 and had to be modified to work with 1.0.1:

                  Evo 1.0 code
                  <?php
                  echo <<<END
                  <!-- some html -->
                  END;
                  
                  // more php here
                  
                  echo <<<END
                  <!-- some html -->
                  END;
                  ?>


                  modified for Evo 1.0.1
                  <?php
                  echo '
                  <!-- some html -->
                  ';
                  
                  // more php here
                  
                  echo '
                  <!-- some html -->
                  ';
                  ?>


                  All working now smiley
                    Jeremy Foster