We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    Quote from: sottwell at Apr 09, 2010, 01:26 PM

    I suppose that’s why I just presumed it didn’t work that way; it’s never occurred to me to do it.

    Me too. I was just as surprised as you were. smiley
      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
      • 22303 MODX Staff
      • 10,725 Posts
      It’s supposed to work with embedded PHP tags, and always did up until changes to the cache sync processor were made in 1.0.1. I thought those changes had been reverted for 1.0.3, and if certain structures are not working in 1.0.3, then it is a bug and needs to be reported and fixed ASAP.
        • 13226
        • 953 Posts
        @ OpenGeek

        That’s a great help to know smiley

        I would add this as a bug, but I don’t know how the reporting system works.

        cheers

        edit:

        Have reported in Jira MODX-1856

        I hope I’ve reported it OK smiley
          • 13226
          • 953 Posts
          Will this bug be fixed before 1.0.4 is released ?

          Or is there a workaround to get it working ?

          Cheers
            • 22303 MODX Staff
            • 10,725 Posts
            The workaround solution prior to 1.0.3 was to replace the cache_sync processor with the one from 1.0.0; I do not know if that will work with 1.0.3. You’ll just have to try it.
              • 13226
              • 953 Posts
              @OpenGeek

              Seems to be working - cheers

              If the following is of interest:

              I took a closer look at the file and found the following changes that have been made in the 1.0.3 cache_sync processor.

              Lines: 171 to 176
              // clean comments from snippet code
              $code = '<?php ' . trim($tmp1['snippet']);
              $code = trim($tmp1['snippet']);
              $code = extension_loaded('tokenizer') ? strip_tokens($code) : $code;
              $code = str_replace('<?php ', '', $code);
              $tmpPHP .= '$s[\''.$modx->db->escape($tmp1['name']).'\']'." = '".$this->escapeSingleQuotes($code)."';\n";
              


              Lines: 192 to 193
              $code =& $tmp1['plugincode'];
              $code = extension_loaded('tokenizer') ? strip_tokens($code) : $code;
              


              Lines: 219 to 240 (Version 1.0.0 starts at line 212)
              // close and write the file
              $tmpPHP .= '?>';
              $filename = $this->cachePath.'siteCache.idx.php';
              
              // invoke OnBeforeCacheUpdate event
              if ($modx) $modx->invokeEvent('OnBeforeCacheUpdate');
              
              if (!$handle = fopen($filename, 'w')) {
                  echo 'Cannot open file (',$filename,')';
                  exit;
              }
              
              // Write $somecontent to our opened file.
              if (fwrite($handle, $tmpPHP) === FALSE) {
                  echo 'Cannot write main MODx cache file! Make sure the assets/cache directory is writable!';
                  exit;
              }
              fclose($handle);
              unset($tmpPHP);
              
              // invoke OnCacheUpdate event
              if ($modx) $modx->invokeEvent('OnCacheUpdate');
              


              Lines: 314 to 359
              /**
               * strip_tokens
               *
               * can be used to remove comments from php code
               * 
               * from http://www.php.net/manual/en/function.php-strip-whitespace.php#91065
               * 
               * @param string $code
               * @return string code with certain php tokens removed (defaults to comments)
               */
              function strip_tokens($code) {
              
                  $args = func_get_args();
                  $arg_count = count($args);
                 
                  // if no tokens to strip have been specified then strip comments by default
                  if( $arg_count === 1 ) {
                      $args[1] = T_COMMENT;
                      $args[2] = T_DOC_COMMENT;
                  }
              
                  // build a keyed array of tokens to strip
                  for( $i = 1; $i < count($args); ++$i )
                      $strip[ $args[$i] ] = true;
              
                  // set a keyed array of newline characters used to preserve line numbering
                  $newlines = array("\n" => true, "\r" => true);
                  $tokens = @token_get_all($code);
                  reset($tokens);
                  $return = '';
                  $token = current($tokens);
                  while( $token ) {
                      if( !is_array($token) )
                          $return.= $token;
                      elseif(    !isset($strip[ $token[0] ]) )
                          $return.= $token[1];
                      else {
                          // return only the token's newline characters to preserve line numbering
                          for( $i = 0, $token_length = strlen($token[1]); $i < $token_length; ++$i )
                              if( isset($newlines[ $token[1][$i] ]) )
                                  $return.= $token[1][$i];
                      }
                      $token = next($tokens);
                  } // while more tokens
                  return $return;
              }
              


              Cheers and hopefully this will be sorted soon in the official distribution package.
                • 21257 MODX Staff
                • 730 Posts
                In the first block of code
                $code = str_replace('<?php ', '', $code);
                

                .. it’s assuming that there is only one <?php, the one that it just added at the beginning. That should be replaced by
                preg_replace('/^\<\?php/', '', $code);
                

                or similar. (Off top of head, not tested.)
                  Mike Schell
                  Lead Developer, MODX Cloud
                  Email: [email protected]
                  GitHub: https://github.com/netProphET/
                  Twitter: @mkschell
                  • 11927
                  • 105 Posts
                  I have seen similar issues for 1.0.3.

                  http://modxcms.com/forums/index.php?topic=50530.0

                  When the php is all on one line it doesn’t work unless you are using a short php tag.

                  So this doesn’t work.
                  <?php echo $var; ?>



                  But these do.
                  <? echo $var; ?>
                  <?php
                  echo $var;
                  ?>


                  I know now that I shouldn’t switch back and forth from php to html in a snippet, but it sure is easier to read when using a program like Notepad++ that highlights the end tag for things when you do switch back and forth. I also know that I shouldn’t use short php tags in MODx, but I didn’t know that until recently, but again it worked before and made it easier to read when editing.
                    You may or may not want to use the code I write. It&#39;s probably all against the syntax rules of php and MODx. smiley

                    Carpet Cleaning