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

    here is a plugin to use if you’re tired to copy and paste template html code in the manager.
    This will allow you to get template code from the file system.
    It has not been deeply tested but seems to work
    Let me know if you find out side effects.

    :-)


    How does it works:
    In the manager, write in the text area the path to your template file from /assets/templates/ prefixing with @FILE:
    => @FILE:myfolderTemplate/index.html

    INSTALLATION:
    Create a new plugin name fileFromTemplate (or any name coming to your mind)
    Copy and paste the code below.
    In the event tabs check the OnLoadWebDocument

    TODO: we could load the lang array to manage error message or use plugins params to customize it

    //Author Helio 01/27/09
    //plugin to load a template from the file system
    $e = & $modx->Event;
    
    switch($e->name){
    
    	case 'OnLoadWebDocument':
    	$tplContent = $modx->documentContent;
    	if( substr($tplContent,0,6) == "@FILE:"){
    		$templatePath = substr($tplContent,6);
    		$toFile = $modx->config['base_path'] ."assets/templates/".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;
    
    }
    
    
      Made with MODx : [url=http://www.copadel.com]copadel, fruits et l
      • 20413
      • 2,877 Posts
        @hawproductions | http://mrhaw.com/

        Infograph: MODX Advanced Install in 7 steps:
        http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

        Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
        http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
        • 16183
        • 1,390 Posts
        Brilliant! thx Helio for the plugin!

        @mrhaw - thx for the link! it’s only recently somebody asked about this type of thing and I couldn’t find a link to direct him/her to.

        cheers/k
          • 7231
          • 4,205 Posts
          So with this plugin I could simply use @FILE:myfolderTemplate/index.html in the code text box for the template? That is way cool.

          Could I add markup above / bellow the include?
          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml"><head>
          	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
          	<meta name="description" content="Post reply" />
          	<meta name="keywords" content="MODx, PHP, application, framework, CMS, content, management, system, web, 2.0, ajax, open, source" />
          	<meta name="robots" content="index,follow" />
          
          @FILE:myfolderTemplate/index.html
          
          <div id="footer">
          some footer here
          </div>


          What about adding multiple files, is that possible?
          @FILE:myfolderTemplate/header.html
          @FILE:myfolderTemplate/body.html
          @FILE:myfolderTemplate/footer.html
            [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

            Something is happening here, but you don&#39;t know what it is.
            Do you, Mr. Jones? - [bob dylan]
            • 34017
            • 898 Posts
            you could do the multiple file thing with Include snippet as mentioned above. dont know about the plugin.
              Chuck the Trukk
              ProWebscape.com :: Nashville-WebDesign.com
              - - - - - - - -
              What are TV's? Here's some info below.
              http://modxcms.com/forums/index.php/topic,21081.msg159009.html#msg1590091
              http://modxcms.com/forums/index.php/topic,14957.msg97008.html#msg97008
              • 11975
              • 2,542 Posts
              @dev_cw

              Here is a recursive version smiley
              Let me know if it works.

              $basefolder = (isset($basefolder)) ? $basefolder : $modx->config['base_path'] ."assets/templates/"; //not yet supported
              $pattern = "~@FILE:(.*)~";
              
              $e = & $modx->Event;
              
              switch($e->name){
              
              	case 'OnLoadWebDocument':
              	$tplContent = $modx->documentContent;
              	$modx->documentContent = parseExternalTpl($pattern,$tplContent,$basefolder);
              	break; 
              	
              	default:
              	break;
              
              }
              
              function parseExternalTpl($pattern,$tplContent,$basePath){
              
              	if(preg_match_all($pattern,$tplContent,$matches)){
              		$tot = count($matches[0]);		
              		for($i=0;$i<$tot;$i++){
              			$result[$matches[0][$i]] = parseExternalTpl($pattern,loadExternalContent($basePath,$matches[1][$i]),$basePath);
              		}
              		$output = str_replace(array_keys($result),array_values($result),$tplContent);
              	}else{
              		$output = $tplContent;
              	}
              	
              	return $output;
              }
              
              function loadExternalContent($basePath,$fileName){
              
              	$toFile = $basePath.trim($fileName); 
              	
              		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();
              			
              		}else{
              			$ob_contents = "Erreur lors du chargement du template => ".$toFile;
              		}
              		
              	return $ob_contents;
              
              }
              
              


                Made with MODx : [url=http://www.copadel.com]copadel, fruits et l
                • 15001
                • 697 Posts
                Hello Helio,

                Could you tell a few words about what you mean by "recursive version" and what it offers more than the "standard" version ?
                  • 20413
                  • 2,877 Posts
                  That means you can add multiple @File: if I’m right!
                  This is really nice!! cool
                    @hawproductions | http://mrhaw.com/

                    Infograph: MODX Advanced Install in 7 steps:
                    http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

                    Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
                    http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
                    • 11975
                    • 2,542 Posts
                    Hi,

                    that’is !

                    And you can also embed @FILE in your included template.

                    For instance:
                    =>index.html

                    @FILE:header.tpl
                    @FILE:content.tpl
                    @FILE:footer.tpl

                    and in content.tpl, you can write

                    @FILE:main.tpl
                    @FILE:left.tpl
                    @FILE:right.tpl

                    Please Note that if by mistake you call from content.tpl @FILE:index.tpl this will cause an infinite loop.
                    I also advice you to change @FILE: to @TPL or whatever you want if you use @FILE syntax in your WF or Ditto call.
                    To do so change the pattern value => "~@TPL:(.*)~"

                    :-)
                      Made with MODx : [url=http://www.copadel.com]copadel, fruits et l
                      • 25663 MODX Staff
                      • 12,272 Posts
                      Can the same be done for snippets, TVs and chunks? And if so anyone want to run a set of benchmarks to see how it compares to DB-based storage? This is really interesting stuff. (probably needs a recursion limit to keep it from infinitely looping, too.)

                      Very nice!
                        Ryan Thrash, MODX Co-Founder
                        Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me