We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9050
    • 13 Posts
    Hi there,
    a simple question:
    why are templates stored to the database?
    It is much more easy to edit a template when its stored in the file system.

    In cmsimple theres a way to call templates from file system via an include.

    Is there a similar way in modx?


      • 22303 MODX Staff
      • 10,725 Posts
      Templates are stored to the database because the folks who originally designed Etomite did it that way. Content in the database vs. the filesystem is an argument as old as content management. I personally keep copies of all my templates in the filesystem and edit those, then copy and paste in the manager. But it’s just as easy to write a snippet to include your templates from the file system, then simply use a single template in MODx to wrap it with anything else you might not want in the template (might be a blank template with nothing but a snippet call in it). I believe someone has done something similar and posted about it here, but I’d have to search for it.
        • 7923
        • 4,213 Posts
        There is allready ready made snippet for this (to have templates/snippets/whatever in filesystem and just include them at manager), and there is also discussion about this in some threads, see here for example and do a seach with "include" for more information.


          "He can have a lollipop any time he wants to. That's what it means to be a programmer."
          • 18436
          • 135 Posts
          My approach is to create a "GLOBAL" template, well that’s what I called it anyway, containing the following:

          [[includeFile?&phpfile=[*templatePath*]/template/template.php]]


          Within a document I have a template variable called templatePath that I’ve setup with @INHERIT as the default property. I then set each master folder or documents "templatePath" TV to something like "assets/issues/march" or whatever you want to call it.

          In the file system I have "assets/issues/march/template/template.php" and sub-folders containing all my css and images. Within the php template I use the TV value for linking to stylesheets and images, and in certain cases have found that I need to set a php path variable as well.

          Not sure how complicated your setup is going to be but I’ve found this works really well for me. You see I’m doing a monthly ezine and each issue has a different feel so managing multiple templates with the Manager was a nightmare, with this approach I use a single template and change the path to what it includes - sweet - no more stressing!!!

          If you need some more help please don’t hesitate to ask.
            • 9050
            • 13 Posts
            Quote from: Marschant at May 02, 2006, 04:44 AM

            My approach is to create a "GLOBAL" template, well that’s what I called it anyway, containing the following:

            [[includeFile?&phpfile=[*templatePath*]/template/template.php]]



            Ups, one month ago - but many thanks, this seems to be a good solution!

            Greetings
            E.
              • 11505
              • 12 Posts
              I am trying to implement this with not much luck. Can you describe the process, what goes where to make this call? TIA
                • 18436
                • 135 Posts
                Quote from: gunner at Jun 07, 2006, 03:49 PM

                I am trying to implement this with not much luck. Can you describe the process, what goes where to make this call? TIA

                My approach is the following:

                • Create a new directory within the assets/templates folder
                • Create your master template as template.php or whatever you want to call it. This is your html with any references to stylesheets, scripts, template variables or placeholders within it.
                • Search for the includeFile snippet and install it or use the code pasted below
                • Setup a new template within the manager insert a includeFile snippet call (see below)
                • Modify any existing template variables within the manager that will need access to this template
                • Create a new document that uses the newly created template, add some content, publish and browse to it, hopefully everything worked out well

                includeFile Snippet
                # For example to include cutenews, you can use
                # [[includeFile?template=nicetemplate&phpfile=cutenews/show_news.php]].
                # You can offcourse pass any variable to the snippet to use for the included file.
                
                if ( !isset($phpfile) || $phpfile == "" ) return "No file specified."; //check if there's a file given.
                
                //Start the buffer
                ob_start();
                
                //include
                include $phpfile;
                
                //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;
                


                snippet call within a newly defined template in the manager

                [[includeFile?&phpfile=[*templatePath*]/template/template.php]]
                


                or

                [[includeFile?&phpfile=assets/templates/mytemplate/template.php]]
                


                I use a templatePath variable because I’ve moved all my resources to "assets/issues/year/month" because each monthly issue I publish has a different look and feel to it. You probabely won’t need to worry about something like this.

                If you need me to post the actual content of template.php please let me know.
                  • 11505
                  • 12 Posts
                  Worked perfectly! Thanks so much.
                    • 18436
                    • 135 Posts
                    Glad I could help!
                      • 19499
                      • 30 Posts
                      I was wondering if you could link the template code, and got directed here by the ever helpfull Sottwell.

                      Thanks Marschant, read your post and even though I could not get it to work the way you described (my issues!) I found it helped my brain spark with some new ideas.

                      I re-read some of the MODx documentation and found the TV @FILE.

                      What I did for a solution was:

                      1. Create a TV called [*templatePath*]
                      2. Default value @FILE assets/templates/index.html (or to where you file is)
                      3. Set the Template Access
                      4. Then put the TV [*templatePath*] in your template code

                      The great thing about this is you can then edit your code within a text editor and because it is linked you do not have to continue to cut and paste all the time.

                      This works perfectly for me and my workflow.

                      I use Style Master and Skedit.

                      Stylemaster lets you use any on line document to preview what your CSS looks like from with Stylemaster app itself. So the way I can work is:

                      1. Linked template code to MODx.
                      2. Style master uses localhost/mysite/index.php?id=4 (non_friendly) as my preview doc.
                      3. Skedit to edit code.

                      So it works as a nice little loop. However I am having trouble with the page sticking in the cache.

                      Any suggestions to this would be great!

                      Thanks all.