We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 15001
    • 697 Posts
    Hi,

    Here’s a improved version of heliotrope’s LoadTemplate plugin.

    LoadTemplate allows to load the template code from a file.
    This improved version allows to specify in a chunk the beginning of the path to the file.
    This can be useful when making sub-sites.

    Heliotrope’s original code can still be found here:
    http://modxcms.com/forums/index.php/topic,31819.msg197423.html#msg197423
    // [FR]
    // Plugin pour charger un template depuis le système de fichiers.
    // Code original par heliotrope.
    // A associer avec l'événement OnLoadWebDocument.
    //
    //
    // [EN]
    // Plugin to load a template from the file system.
    // Original code by heliotrope.
    
    $e = & $modx->Event;
    
    switch($e->name){
    
        case 'OnLoadWebDocument':
        $tplContent = $modx->documentContent;
        if( substr($tplContent,0,6) == "@FILE:"){
        
            // Improvement by ALTIPOINT development (JR)
            // Last update: July 21, 2009
            //
            // [FR]
            // Permet de placer un chunk au début de la chaîne menant au modèle.
            // Avantage : permet de rediriger dynamiquement vers 
            // un dossier correspondant à un sous-site.
            //
            // Au final, la chaîne vers le modèle doit être complète,
            // incluant assets/templates si le modèle se trouve dans ces sous-dossiers.
            //
            // [EN]
            // Allows to use a chunk at the beginning of the path to the template file.
            // Benefit : useful to redirect to subsites.
            //
            // ---------------------------------------------
            // Example: 
            // To load template from "main_site_root/subsite1/assets/templates/subsite.tpl"
            // Put in the code text area of your template :
            // @FILE:{{subsite1_root}}/assets/templates/subsite.tpl
            //
            // where the subsite1_root chunk contains "subsite1" (without the quotes).
    
            if (substr($tplContent,6,2) == "{{") { // the path begins with a chunk
                $chunkCloseBracketsPos = strpos($tplContent,'}}');
                if ($chunkCloseBracketsPos > 9) { // chunk seems valid
                    $myChunk = substr($tplContent,8,$chunkCloseBracketsPos-8);  // extract the chunk name
                    $myChunk = $modx->getChunk($myChunk);
                    $templatePath = $myChunk.substr($tplContent,$chunkCloseBracketsPos+2);
                } else { // chunk is invalid
                return 'Could not load template. Check path to template.';  
                }
             } else { // the path to template is a standard path, 
                      // i.e. not beginning with a chunk
                $templatePath = substr($tplContent,6);
             }
             
             // ---- End of improvement ----
             
            $toFile = $modx->config['base_path'].trim($templatePath); 
            if(is_file($toFile)){
                //Start the buffer
                ob_start();
                include $toFile;
                //get contents from the buffer
                $ob_contents = ob_get_contents();
                //and kill/delete the buffer
                ob_end_clean();
                //return it to MODx
                $modx->documentContent = $ob_contents;
            }else{
                $modx->documentContent = "Erreur lors du chargement du template => ".$toFile;
            }
        }
        break; 
        
        default:
        break;
    
    }
    
      • 5689
      • 289 Posts
      This sounds pretty nice. Where I would put the chunk to load the template file? Would it just go in the MODx template code itself?
        I'm learning more about MODx all the time and loving it.
        • 15001
        • 697 Posts
        Yes.

        With original version, you had to do this.
        1) Create new plugin LoadTemplate and paste the plugin code in it.
        2) Check the OnLoadWebDocument checkbox on the plugin System Events tab.
        3) Put a string like thus which follows in your template source code :
        @FILE:helpPagesTemplate.tpl

        The path was implicitely relative to the assets/templates subfolder.
        For instance
        @FILE:help_templates/helpInEnglish.tpl

        was used to specify path to
        /assets/templates/help_templates/helpInEnglish.tpl


        With enhanced version, steps 1 and 2 are the same.
        For step 3, you have to specify assets/templates explicitely.
        But, --and that’s the benefit-- you can use a chunk at the beginning of your path.

        For instance, if you created a folder at the root of your site for particular customer, you could use :
        @FILE:{{customer}}/assets/templates/template.tpl

        where the chunk {{customer}} could be set to "demo_123" or whatever else.
        (Of course, the chunk can also contain the assets/templates subpath if you prefer.

        So you can run demos on your site. If you migrate the"demo_123" folder (containing the subsite), you can set the {{customer}} so that it doesn’t have any more content on the production server.
        This way you can map a MODx-like structure contained in demo_123 to the structure starting at the root of a production server.

        Hope this answers your question and helps understanding the reason of the enhancement.