We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 13629
    • 23 Posts
    I’ve been using MODx for a little while now and I’ve known from the start that editing PHP/HTML code inside of textareas is a ridiculous way to go. I want to somehow separate these files so I can edit them in an actual text editor and put them under source control.

    What’s the best way to do this? I can think of some ways, but nothing that strikes me as a good idea.

    Thanks,
    Jason
      • 25663 MODX Staff
      • 12,272 Posts
      We do this all the time for custom web apps.

      <?php
      // &file=`assets/foo/bar.php` indicates the file to include
      // &return=`1` return explicit content from return statement in included file
      // default behavior is to use output buffering to capture the content from the include
      
      $output = '';
      if (isset($file) && file_exists($file)) {
          if (!isset($return) || $return == false) {
              ob_start();
              include ($modx->config['base_path'] . $file);
              $output = ob_get_contents();
              ob_end_clean();
          } else {
              $output = include ($modx->config['base_path'] . $file);
          }
      }
      return $output;
      ?>
      
        Ryan Thrash, MODX Co-Founder
        Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
        • 13629
        • 23 Posts
        Thanks for the reply. I understand what that code does and this may be a dumb question, but where exactly does that code go? As far as I understand, PHP can only be run inside of snippets. What do I do for chunks and templates?

        Or is that the code for a snippet that includes an external file?
          • 22303 MODX Staff
          • 10,725 Posts
          Quote from: jasonswett at Oct 17, 2008, 09:26 AM

          Thanks for the reply. I understand what that code does and this may be a dumb question, but where exactly does that code go? As far as I understand, PHP can only be run inside of snippets. What do I do for chunks and templates?

          Or is that the code for a snippet that includes an external file?
          If you are willing to treat chunks and templates as simple includes from a snippet, you could use this for that as well, but yes, this is the code for a snippet that includes an external file. You can theoretically build your whole site with one snippet in the manager this way, but this may circumvent certain [existing 3rd party] components that expect things to be stored in the DB, or certain parts of the caching mechanisms.

          In future releases (of the Revolution/2.0 branch) we’ll have native ways to store the actual content of any MODx resource or element (i.e. document or template/tv/chunk/snippet/plugin/module) on the file system rather than directly in the database. The field in the table where content would normally be stored will be just a pointer to the static file artifact.