We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7842
    • 8 Posts
    Maybe I’m just fighting against a good idea, but I’m a general web developer with a good bit of experience coding front-end stuff. Basically, I want to edit my templates in a proper editor and store them as files instead of in the database so I can using things like version control and not have to mess with constantly copy-and-pasting into the DB. The way I have managed this in the past with modx Evolution was to use a snippet that included a file with php and then calling that snippet in a template, passing it the right filename.

    Is that basically the best way to do it? I’m using Revolution now. Am I totally missing the point? Is there any way built in to do things the way I want to?

    Thanks

      • 3749
      • 24,544 Posts
      Quote from: donut2d at Sep 23, 2010, 01:20 PM

      Is that basically the best way to do it? I’m using Revolution now. Am I totally missing the point? Is there any way built in to do things the way I want to?
      Thanks

      Not at present, but your snippet should still work in Revolution as long as you change the snippet tag format (though I confess that I haven’t tried it):

      [[!SnippetName]]
        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
        • 29089
        • 13 Posts
        I just do local version control... :/
        I use my IDE which controls all versions of my documents, when im ready to whack it into my live environment I just copy and paste. I never use the cms for editing code (unless its a trivial spelling mistake or a bracket misplaced etc..).
          • 5340
          • 1,624 Posts
          I have this so far

          <?php   
              $cfg = $modx->getConfig();
              
              $pathToFile = MODX_BASE_PATH . $cfg['templates_path'] . $modx->getOption('path',$scriptProperties,'default');
          
              $o = '';
              $o = file_get_contents($pathToFile);
           
          
              if ($o){
                  return $o;
              }else{
                  return "Template not found! " . $pathToFile;
              }



          Usage

          [[!IncludeTemplate? &path=`home.tpl`]]


          Note: templates_path should be defined as a system setting for your context. EG: YOUR_SITE.com/assets/templates/
          I’m working on a solution that reads the context path so you don’t have to put YOUR_SITE.com in the path.


            • 3749
            • 24,544 Posts
            By templates, I assume you mean Tpl Chunks. Templates are elements and you’d have to do more coding to store them in files at this point.

            I think you might be better off creating a context setting called base_path and using this:

            $pathToFile = $modx->getOption('base_path') . $modx->getOption('templates_path') . $modx->getOption('path',$scriptProperties);


            You wouldn’t need getConfig() and you wouldn’t need the base path in your templates_path string.

            Context settings will override any system settings so MODx would get the proper base_path for each context.

            I assume you know that standard snippets like Wayfinder and getResources won’t use your files because they expect the name of the Tpl Chunk rather than its path.

            I’m not recommending this, but In theory, you could rewrite $modx->getChunk() to get your Tpl Chunk files if they exist instead of pulling them from the DB. Then all snippets would get your files and process them normally and you could just use getChunk() in your own snippets. The changes could be overwritten when you upgrade, though.






              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
              • 27708 MODX Staff
              • 2,502 Posts
              You can review this short article here from the Docs: http://rtfm.modx.com/display/revolution20/Managing+Resources+and+Elements+via+SVN

              I am pretty sure this will work:

              Your Include snippet will be:
              <?php
              if (!file_exists($file)) return '';  
              $o = include $file;  
              return $o;  

              You snippet call would be:
              [[include? &file=`/path/to/my/svn/checkout/snippet.php`]]


                Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub
                • 7842
                • 8 Posts
                Thanks for the suggestions, everyone. Most everyone suggested a variation of what I’m already doing. I was actually looking to see if there was a better way, but I guess it’s the best/only way. Here’s my LoadTemplate snippet:
                <?php
                $config = $modx->getConfig();
                $path   = $config['filemanager_path'] . "assets/templates/$template_theme/layout.php";
                
                return (file_exists($path))
                    ? include $path
                    : "Could not find template at `$path`.";

                  • 1169
                  • 312 Posts
                  @ smashingred I tried your include snippet call to check out developing in Netbeans I found it added a 1 to the end of every chunk call, template calls seemed ok.

                  Revo 2.0.2 Advanced Install
                  I am on Win XP
                  localhost
                  wamp
                  Apache 2.2.1
                  Php 5.3
                  Mysql 5.1.36

                  I copied and pasted your code and changed path to local git repo.

                  It was a lot of trouble IMHO to do what I normally do in database and still did not work.

                  I do not usually use an IDE for template editing but thought I would give it a try.
                    DEVELOPMENT ENV:- Ubuntu 12.04 | MODx Revolution 2.2.8 | LAMP 2i Apache 2.2.22 | Php 5.3.10 | Mysql 5.5.31 MySQL client version: 5.5.31
                    • 3749
                    • 24,544 Posts
                    Quote from: allanb at Sep 29, 2010, 04:53 PM

                    @ smashingred I tried your include snippet call to check out developing in Netbeans I found it added a 1 to the end of every chunk call, template calls seemed ok.

                    @ allenb, if the snippet your are "including" has a return statement, the value it returns will replace the tag. If it doesn’t have a return statement, however, the tag will be replaced with ’1’ indicating a successful include.
                      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
                      • 1169
                      • 312 Posts
                      This is the post I took my code from.

                      Quote from: smashingred at Sep 27, 2010, 07:55 PM

                      You can review this short article here from the Docs: http://rtfm.modx.com/display/revolution20/Managing+Resources+and+Elements+via+SVN

                      I am pretty sure this will work:

                      Your Include snippet will be:
                      <?php
                      if (!file_exists($file)) return '';  
                      $o = include $file;  
                      return $o;  

                      You snippet call would be:
                      [[include? &file=`/path/to/my/svn/checkout/snippet.php`]]



                      And as you can see it has a return statement.
                      It does return the contents of the chunk plus the ’1’
                      I found two ’1’s in the displayed resource page with two calls to the ’include’ snippet
                      Could this be a bug. huh
                      Quote from: allanb at Sep 29, 2010, 04:53 PM

                      My Details
                      Revo 2.0.2 Advanced Install
                      I am on Win XP
                      localhost
                      wamp
                      Apache 2.2.1
                      Php 5.3
                      Mysql 5.1.36

                      I copied and pasted your code and changed path to local git repo.


                        DEVELOPMENT ENV:- Ubuntu 12.04 | MODx Revolution 2.2.8 | LAMP 2i Apache 2.2.22 | Php 5.3.10 | Mysql 5.5.31 MySQL client version: 5.5.31