We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33423
    • 27 Posts
    Is it possible to define external template file instead of fetching it from DB for ModX.
    Because i wanna edit it with an external editor it’s too slow to edit from a textarea.
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Yes, create a snippet with the code to include() your external file. Then create a new template with nothing in it but the snippet’s tags.
        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
        • 33423
        • 27 Posts
        sometimes, i can not believe, how flexible modx is.

        Thanks so much...
          • 8960
          • 150 Posts
          Actually, in another post/thread, Susan said it best when she referred to a Snippet called IncludeFile. I use this on all my sites for my templates.

          What: IncludeFile snippet. Learn about it here: http://modxcms.com/forums/index.php/topic,4139.msg30260.html#msg30260
          Why: This will allow you to add the following line in any template you create within the Manager and edit the file locally, say with Dreamweaver, which will let you edit the template "live" on your MODx site:
          [[includeFile? &file=`assets/templates/templatename/index.html`]]

          How: Create a Snippet called includeFile. Grab the code I used here, and paste it in the snippet:
          <?php
          # Snippet to include files from filesystem
          # [[includeFile? &file=`assets/templates/mytemplate/file.html`]]
          
          if ( !isset($file) || $file== "" ) return "No file specified."; //check if there's a file given.
          
          //Start the buffer
          ob_start();
          
          //include
          include $file;
          
          //get contents from the buffer
          $ob_contents = ob_get_contents();
          
          //and kill/delete the buffer
          ob_end_clean();
          
          //return it to MODx
          return $ob_contents;
          ?>
            • 33423
            • 27 Posts
            Thanks so much laugh
              • 8217
              • 3 Posts
              Hi All,

              This is exactly what I want to do as I find template development much easier if I keep it open in Dreamweaver and save changed to the file rather than having to update the DB.

              I’ve tried setting up the snippet as described above, but I get the snippet code showing up when I view the page. Ie:

              [[includeFile? &file=`assets/templates/glasshouse/index.html`]]

              What I find strange is that if I put an incorrect filename in (remove the "i" from index.html) I get an error as below:

              « MODx Parse Error »
              MODx encountered the following error while attempting to parse the requested resource:
              « PHP Parse Error »

              PHP error debug
              Error: include(assets/templates/glasshouse/ndex.html) [function.include]: failed to open stream: No such file or directory
              Error type/ Nr.: Warning - 2
              File: C:\webroot\glasshouseII\manager\includes\document.parser.class.inc.php(769) : eval()’d code
              Line: 11

              What am I doing wrong? Thanks.
                • 8217
                • 3 Posts
                OK - I’ve solved this by removing all the output buffering stuff in the snippet. It’s effectively now just an "include".

                It seems to me it should be fine to run it like this, but now I’m wondering why all that was there in the first place. My PHP ain’t bad, but I just can’t see why all those output buffer commands were necessary in the first place.

                Anybody able to enlighten me on this one?
                  • 42562
                  • 1,145 Posts
                  After five years, no body?

                  I find this curious too, about the buffer.
                  Does a pure single include line not do the job as effectively?
                    TinymceWrapper: Complete back/frontend content solution.
                    Harden your MODX site by passwording your three main folders: core, manager, connectors and renaming your assets (thank me later!)
                    5 ways to sniff / hack your own sites; even with renamed/hidden folders, burst them all up, to see how secure you are not.